2012-07-04 104 views
0

我尝试在ICS上以编程方式激活或停用Android Beam功能,但找不到任何API。可能吗 ?Android Beam - 以编程方式激活

我想知道在启动推送操作之前是否启用了Android Beam功能。可能吗 ?

回答

2

在手机的设置中,您可以启用和禁用Android Beam功能(无线网络 - >更多... - > Android Beam)。普通应用程序没有必要的权限来打开或关闭此功能(并且没有API)。但是,您可以使用new Intent(Settings.ACTION_WIRELESS_SETTINGS)直接从您的应用发送和意图打开此设置屏幕。

在Android 4.1 JB上,添加了一个新的API调用NfcAdapter.isNdefPushEnabled(),以检查Android Beam是打开还是关闭。

顺便说一句:即使Android Beam被禁用,只要NFC开启,您的设备仍然能够接收Beam消息。

+1

使用'新的意图(Settings.ACTION_NFCSHARING_SETTINGS)'为使用户在Android Beam设置。 NFC家伙建议的那个,会带你进入NFC设置(这也很有用)。 – Dennis

0

您可以根据Android版本和当前状态来具体选择要调出哪个设置屏幕。以下是我做的:

import android.annotation.TargetApi; 
import android.app.Activity; 
import android.content.Intent; 
import android.nfc.NfcAdapter; 
import android.os.Build; 
import android.os.Bundle; 
import android.provider.Settings; 

@TargetApi(14) 
// aka Android 4.0 aka Ice Cream Sandwich 
public class NfcNotEnabledActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     final Intent intent = new Intent(); 
     if (Build.VERSION.SDK_INT >= 16) { 
      /* 
      * ACTION_NFC_SETTINGS was added in 4.1 aka Jelly Bean MR1 as a 
      * separate thing from ACTION_NFCSHARING_SETTINGS. It is now 
      * possible to have NFC enabled, but not "Android Beam", which is 
      * needed for NDEF. Therefore, we detect the current state of NFC, 
      * and steer the user accordingly. 
      */ 
      if (NfcAdapter.getDefaultAdapter(this).isEnabled()) 
       intent.setAction(Settings.ACTION_NFCSHARING_SETTINGS); 
      else 
       intent.setAction(Settings.ACTION_NFC_SETTINGS); 
     } else if (Build.VERSION.SDK_INT >= 14) { 
      // this API was added in 4.0 aka Ice Cream Sandwich 
      intent.setAction(Settings.ACTION_NFCSHARING_SETTINGS); 
     } else { 
      // no NFC support, so nothing to do here 
      finish(); 
      return; 
     } 
     startActivity(intent); 
     finish(); 
    } 
} 

(在此,我把这段代码到公共领域,不需要任何许可条款或属性)