2015-11-17 41 views
0

我有兩項活動。我想使用第一個活動(如MainAcitivity)來讀卡,第二個活動是寫卡。因爲當發現卡時活動需要處於活動狀態。因此,我用兩個活動如下設置:如何在不同的活動中讀取/寫入NFC卡

 </intent-filter> 
     <!-- Handle notes detected from outside our application --> 
     <intent-filter> 
      <action android:name="android.nfc.action.NDEF_DISCOVERED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="text/plain" /> 
     </intent-filter> 

然而,我的問題是,當我在第二個活動,我掃描NFC卡,手機會顯示雙方的意向選擇器,第一和第二項活動。

那麼,我如何禁用NDEF_DISCOVERED意向過濾器的第一個活動,而我在第二個活動(和其他方式)的代碼?

這是一個完整AndroidManifest文件:

<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".FirstActivity" 
      android:label="@string/app_name" 
      android:configChanges="orientation|screenSize|screenLayout" 
      > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <!-- Handle notes detected from outside our application --> 
      <intent-filter> 
       <action android:name="android.nfc.action.NDEF_DISCOVERED" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:mimeType="text/plain" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".SecondActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.nfc.action.NDEF_DISCOVERED" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:mimeType="text/plain" /> 
      </intent-filter> 
     </activity> 
    </application> 

回答

0

爲了迫使NFC發現事件由目前在前臺活動收到,你可以使用任何的foreground dispatch systemreader-mode API

如果NFC發現事件當前處於前景中,則這兩種方法都將在接收NFC發現事件時給予優先級。

所以,你可以,例如,使用類似:

public void onResume() { 
    super.onResume(); 
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

    // to catch all NFC discovery events: 
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null); 

    // or to only catch text/plain NDEF records (as you currently do in your manifest): 
    //IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 
    //try { 
    // ndef.addDataType("text/plain"); 
    //} catch (MalformedMimeTypeException e) {} 
    //nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[] { ndef }, null); 
} 

public void onPause() { 
    super.onPause(); 
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    nfcAdapter.disableForegroundDispatch(this); 
} 

public void onNewIntent(Intent intent) { 
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) || 
     NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) || 
     NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { 
     // handle NFC events ... 
    } 
} 

而且,如果你不希望一個活動是通過NFC發現事件開始,你並不需要聲明一個android.nfc.action.*_DISCOVERED意圖過濾該活動的主要活動。