2012-07-04 54 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(); 
    } 
} 

(在此,我把這段代碼到公共領域,不需要任何許可條款或屬性)