我正在在Android项目中实现NFC。我已经写了AndroidManifest.xml和Java代码中所需的条目。
当任何标签靠近设备时,我的应用程序都会检测标签,然后打开活动,但在这里我获得了NFC标签信息,但onNewIntent
不执行。
请分享您的观点。
public class NFCActivity extends Activity {
private NfcAdapter mNFCAdapter;
private PendingIntent mNfcPendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfc);
// Create the OpenSqliteHelper object. It always best to create only
// once instance of this OpenSqliteHelper
DatabaseManager.init(this);
mNFCAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNFCAdapter == null) {
// Device does not support NFC
Toast.makeText(this,
getString(R.string.device_does_not_support_nfc),
Toast.LENGTH_LONG).show();
} else {
if (!mNFCAdapter.isEnabled()) {
// NFC is disabled
Toast.makeText(this, getString(R.string.enable_nfc),
Toast.LENGTH_LONG).show();
} else {
mNfcPendingIntent = PendingIntent.getActivity(NFCActivity.this,
0, new Intent(NFCActivity.this, NFCActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
}
finish();
}
@Override
protected void onNewIntent(Intent intent) {
Tag mTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
@Override
protected void onPause() {
super.onPause();
mNFCAdapter.disableForegroundDispatch(this);
}
@Override
public void onResume() {
super.onResume();
mNFCAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null,
null);
}
}
androidmanifest.xml
<activity
android:name="net.livepatrols.thepartnerSA.NFCActivity"
android:theme="@android:style/Theme.NoDisplay" >
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
当活动在前景时,您的代码正常工作,但是当活动以android.nfc.action.XXX_DISCOVERED
操作的意图启动时,它将无法正常工作。由于它是第一个意图,因此onNewIntent()
不会被调用,但是您可以在onCreate()
方法中使用意图。您应该从onCreate()
和onNewIntent()
调用NFC逻辑,在访问标签之前验证操作。
public class NFCActivity extends Activity {
private NfcAdapter mNFCAdapter;
private PendingIntent mNfcPendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
performTagOperations(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
performTagOperations(intent);
}
private void performTagOperations(Intent intent){
String action = intent.getAction();
if(action.equals(NfcAdapter.ACTION_TAG_DISCOVERED) ||
action.equals(NfcAdapter.ACTION_TECH_DISCOVERED) ){
//PERFORM TAG OPERATIONS
}
}
...
}
在我的情况下,我遵循nfcAdapter.enableForegroundDispatch(...)
盲目的文档,然后将此代码粘贴到fragment 中:
pendingIntent = PendingIntent.getActivity(
this,
0,
new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
由于它实际上期望活动,只需将所有内容更改为getContext()
,尤其是getClass()
。
pendingIntent = PendingIntent.getActivity(
getContext(),
0,
new Intent(getContext(), getContext().getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);