Android NFC中的Foreground Dispatch系统问题

时间:2012-02-03 09:24:42

标签: android nfc

我想在少数其他具有以下功能的NFC应用程序中制作一个特定的NFC应用程序 1.只有在设备上扫描具有特定文本(NDEF类型)的NFC标签时,才应启动应用程序。 (这意味着如果点击任何其他带有文本类型的标签,则不应显示我的应用程序)

  1. 启动应用程序时,“使用完整操作”选项和设备中所有NFC应用程序的列表不应该在那里
  2. 在这里,我已经实现了前台调度系统,但它只列出了所有具有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);    
    }
    

    如果有人能告诉我我错过了什么,那就太好了 提前致谢

2 个答案:

答案 0 :(得分:0)

如果您可以控制NFC标签的内容并使用Android 4.0,请考虑在短信后附加Android Application Record

答案 1 :(得分:0)

三条评论:

  1. 您创建并初始化IntentFilter对象并完全使用它。也许您应该删除该代码以保持清晰,因为您已经在AndroidManifest.xml文件中使用了前台调度机制。

  2. 建议您使用Google Play的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) };
    
  4. 然后您可以专注于NFC消息本身而不是活动调度。

相关问题