2012-02-03 20 views
1

我想在少數其他NFC應用程序中製作一個特定的NFC應用程序,它應該具有以下功能:
1.應用程序應僅在具有特定文本的NFC標籤(NDEF類型)在設備上掃描。 (這意味着如果有文本類型的任何其他標記被竊聽我的應用程序不應該顯示)Android NFC中的前臺調度系統問題

  1. 當啓動在設備中的應用「使用的應用」的選項,所有的NFC應用的上市不應該是有

在這裏,我已經實現了前臺調度系統,但它只列出了所有具有NDEF_DISCOVERED優先級的NFC應用程序。

的清單文件的代碼片段如下:

<activity android:name=".NFCDiscovered" 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> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.NFC" /> 
<uses-feature android:name="android.hardware.nfc" android:required="true" /> 

這裏NFCDiscovered.java需要閱讀與前景調度系統特定文本類型的NFC標籤的照顧。代碼片段爲:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.nfcreader); 
    mAdapter = NfcAdapter.getDefaultAdapter(this); 

    mPendingIntent = PendingIntent.getActivity(this, 0,new 
    Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);   
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 
    try { 
     ndef.addDataType("text/plain"); 

    } catch (MalformedMimeTypeException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    mFilters = new IntentFilter[] { ndef }; 

    //resolveIntent(getIntent()); 

} 

public void onResume() { 
    super.onResume(); 
    // TODO Auto-generated method stub 
    TextView myText= (TextView)findViewById(R.id.nfcTxt); 
    super.onResume();   
    if (mAdapter != null) 
     mAdapter.enableForegroundDispatch(this, mPendingIntent, 
      mFilters,null); 




} 
protected void onNewIntent(Intent intent) { 
    NdefMessage[] msgs = null; 
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){ 
     Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
     if (rawMsgs != null) { 
      msgs = new NdefMessage[rawMsgs.length]; 
      for (int i = 0; i < rawMsgs.length; i++) { 
       msgs[i] = (NdefMessage) rawMsgs[i]; 
      } 
     } 

    } 
    List<TextRecord> records = NdefMessageParser.parse(msgs[0]); 
    String text = null; 
    for(TextRecord local:records) 
    { 
     text = local.getText(); 
    } 
    //myText.setText("text :"+msgs); 



} 
public void onPause() {   
    super.onPause();   
    if (mAdapter != null) mAdapter.disableForegroundDispatch(this);  
} 

如果有人能告訴我我失蹤的事情,那將是非常棒的。
在此先感謝

+1

我不清楚你實際上掃描了你的應用程序已經啓動的應用程序的標籤,並且在前臺,因爲你說「應用程序應該只在具有特定文本的NFC標籤上啓動...」 僅當您的應用程序位於前臺時,前臺調度才起作用。如果它處於前景中,那麼您如何篩選意圖或實際啓動的意圖有什麼問題。你可以檢查logcat,看看當你掃描標籤並將它們粘貼在這裏時,啓動了什麼意圖? – robertly 2012-02-03 22:51:53

回答

0

三點意見:

  1. 您創建和初始化對象的IntentFilter並做使用它。也許你應該清除那些代碼,因爲你已經在AndroidManifest.xml文件中使用了前臺調度機制。

  2. 建議使用谷歌從一個NFC閱讀器應用程序播放收集有關您嘗試讀取或寫入NFC標籤中的信息。應用程序可以爲技術列表提供標籤支持和其擁有的內存容量。

  3. 也許你可以從積極的閱讀模式第一次啓動。低於代碼段。

    @Override 
    public void onResume(){ 
    
    super.onResume(); 
    
    // here to scan code. 
    NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    
    // disable foreground dispatch 
    mNfcAdapter.disableForegroundDispatch(this); 
    
    Bundle bundle = new Bundle(); 
    // 
    mNfcAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A, null) }; 
    

那麼你就可以專注於NFC信息本身,而不是活動調度。