2013-10-28 120 views
5

我正在研究一個應用程序,我需要讀取存儲在NFC標籤中的數據,數據我的意思是簡單的整數值,例如0,1,2,3等等。從NFC讀取數據的功能在Activity類中運行良好,但我需要在後臺運行應用程序,所以即使應用程序未在前臺運行,我也可以從NFC讀取數據。所以我寫了一個服務類,並將該函數從Activity移動到了服務類。但它沒有奏效。

Android:讀服務類中的NFC標籤

這是 「MainActivity.java」

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     startService(new Intent(this, MyService.class)); 
    } 

startService(新意圖(此,MyService.class));
此行開始服務類。

的服務類別是 「MyService.java」

public class MyService extends Service { 

    private NfcAdapter mNfcAdapter; 
    private PendingIntent mNfcPendingIntent; 
    private IntentFilter[] mReadTagFilters; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
     mNfcPendingIntent = PendingIntent.getActivity(this, 0, new 
     Intent(this, 
     getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP 
     | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0); 
     IntentFilter ndefDetected = new IntentFilter(
     NfcAdapter.ACTION_NDEF_DISCOVERED); 
     ndefDetected.addDataScheme("http"); 
     IntentFilter techDetected = new IntentFilter(
     NfcAdapter.ACTION_TECH_DISCOVERED);  
     mReadTagFilters = new IntentFilter[] { ndefDetected, techDetected };  
      Log.v("service", "return from onCreate function"); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

    @Override 
    @Deprecated 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 


     Log.v("service", "onStart function"); 
     if (mNfcAdapter != null) { 
      if (!mNfcAdapter.isEnabled()) { 
       new AlertDialog.Builder(this) 
         .setTitle("NFC Dialog") 
         .setMessage("Please switch on NFC") 
         .setPositiveButton("Settings", 
           new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface arg0, 
              int arg1) { 
             Intent setnfc = new Intent(
               Settings.ACTION_WIRELESS_SETTINGS); 
             startActivity(setnfc); 
            } 
           }) 
         .setOnCancelListener(
           new DialogInterface.OnCancelListener() { 
            public void onCancel(DialogInterface dialog) { 
             dialog.dismiss(); 
            } 
           }).create().show(); 
      } 
      mNfcAdapter.enableForegroundDispatch (MainActivity.mContext, mNfcPendingIntent, 
        mReadTagFilters, null); 
     } else { 
      Toast.makeText(this, 
        "Sorry, NFC adapter not available on your device.", 
        Toast.LENGTH_SHORT).show(); 
     } 
     Log.v("service", "return fonStart function"); 
    } 

} 

在此所述的onCreate()函數運行良好。問題是這樣的線:

mNfcAdapter.enableForegroundDispatch (mContext, mNfcPendingIntent, 
        mReadTagFilters, null); 

函數的第一個參數接受一個活動的上下文,這是我在哪裏被卡住。 有沒有什麼辦法來解決這個或任何其他選擇。感謝您的幫助。

回答

3

Android的NFC功能(特別是接收標籤,光束等內容)僅適用於前景活動。

+0

感謝您的回覆,但您能否建議默認android NFC在後臺運行時如何讀取標籤並嘗試使用瀏覽器打開數據? –

+0

NFC標籤檢測由NFC系統服務處理。系統服務根據意圖通知**活動**。 Web瀏覽器就像其他想要接收NFC事件的活動一樣,意圖爲這些意圖註冊過濾器。 –