0

我试图从 android 开发人员那里实现 NFC 查看器的示例代码,但我没有从我的意图中收到任何操作,这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tag_viewer);
    mTagContent = (LinearLayout) findViewById(R.id.list);
    mTitle = (TextView) findViewById(R.id.title);

    final Intent queryIntent = getIntent();  
    final String queryAction = queryIntent.getAction(); 

    if (Intent.ACTION_SEARCH.equals(queryAction)) {  
         onNewIntent(getIntent());
   }  
   else {  
       //nothing
   }

   // resolveIntent(getIntent());

}

void resolveIntent(Intent intent) {
    // Parse the intent
    final String action = intent.getAction();

    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
        // When a tag is discovered we send it to the service to be save. We
        // include a PendingIntent for the service to call back onto. This
        // will cause this activity to be restarted with onNewIntent(). At
        // that time we ryead it from the database and view it.
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage[] msgs;
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        } else {
            // Unknown tag type
            byte[] empty = new byte[] {};
            NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
            NdefMessage msg = new NdefMessage(new NdefRecord[] {record});
            msgs = new NdefMessage[] {msg};
        }
        // Setup the views
      //  setTitle(R.string.);
        buildTagViews(msgs);
    } else {
        Log.e(TAG, "Unknown intent " + intent);
        finish();
        return;
    }
}
@Override
public void onNewIntent(final Intent newIntent) {
    super.onNewIntent(newIntent);
    final Intent queryIntent = getIntent();
    setIntent(queryIntent);
    resolveIntent(queryIntent);
}

和意图过滤器:

    <activity android:name="TagReader_BuiltIn" 
         android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>

意图.getAction(); 总是导致 NULL,有什么想法吗?提前致谢

4

3 回答 3

0

首先检查您的设备是否提供 NFC 功能。

您是否在清单文件中使用了以下权限

<uses-feature android:name="android.hardware.nfc" android:required="false" />
<uses-permission android:name="android.permission.NFC"/>

使用<action android:name="android.nfc.action.NFC_DISCOVERED"/>而不是使用

<action android:name="android.nfc.action.TAG_DISCOVERED"/>

并且不要手动调用 onNewIntent() 方法。像这样在 onResume 中调用方法 resolveIntent(Intent intent)

        super.onResume();      
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) 
        {
          resolveIntent(getIntent());
         }

希望这有帮助..

于 2012-05-07T10:56:56.997 回答
0

确保您的活动清单中有 android:launchMode="singleTop" 。

public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        final Intent queryIntent = getIntent();  
        final String queryAction = queryIntent.getAction();  
        if (Intent.ACTION_SEARCH.equals(queryAction)) {  
             //do something
        }  
        else {  
            //do something
        }  
    }
    void resolveIntent(Intent intent) {
    // Parse the intent
    final String action= intent.getAction();

    .......
}

 @Override  
    public void onNewIntent(final Intent newIntent) {  
        super.onNewIntent(newIntent);  
        final Intent queryIntent = getIntent(); 
        setIntent(queryIntent);
        resolveIntent(queryIntent);
    } 

代替

String action = intent.getAction();
   @Override
public void onNewIntent(Intent intent) {
    setIntent(intent);
    resolveIntent(intent);
}
于 2012-05-07T09:39:02.747 回答
0

如果您的 NFC 芯片有标签,您应该运行以下检查:

if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
// Do something
} 
于 2015-05-05T13:19:00.537 回答