Android 方法onNewIntent(intent)未在NFC中执行
我正在实施android中的NFC项目。我已经编写了AndroidManifest.xml和Java代码中所需的条目 当任何标签靠近设备时,我的应用程序会检测到该标签,然后它会打开活动,但在这里我会获取NFC标签信息,但不会执行 请大家分享你的观点Android 方法onNewIntent(intent)未在NFC中执行,android,nfc,Android,Nfc,我正在实施android中的NFC项目。我已经编写了AndroidManifest.xml和Java代码中所需的条目 当任何标签靠近设备时,我的应用程序会检测到该标签,然后它会打开活动,但在这里我会获取NFC标签信息,但不会执行 请大家分享你的观点 public class NFCActivity extends Activity { private NfcAdapter mNFCAdapter; private PendingIntent mNfcPendingIntent;
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\u发现的操作启动的,则代码无法工作。由于这是第一个意图,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(…)
的文档,并将此代码粘贴到片段中:
pendingIntent = PendingIntent.getActivity(
this,
0,
new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
因为它实际上需要活动,所以只需将所有内容更改为getContext()
,尤其是getClass()
尝试在活动标记android:launchMode=“singleInstance”或android:launchMode=“singleTop”中添加,如果问题与onNewIntent相关,它将解决它已经是singleTop`。addFlags(Intent.FLAG_activity_SINGLE_TOP),0);`此处简短版本为-在意图和清单中设置单个顶部标志。不起作用我尝试了你的观点设置android:launchMode=“singleInstance”或android:launchMode=“singleTop”@Littlenaruto的解决方案对我起作用,但不知道为什么……这毫无意义。为什么要调用performTagOperations
两次?@IgorGanapolsky-这种方法不会调用performTagOperations
两次。这涵盖了两种不同的场景。1) 当活动启动时(如果检测到标记),以及2)当活动是前台活动时。请记住,只有在活动可见时才会调用onNewIntent。enableForegroundDispatch
要求将活动作为参数集getContext()
而不是此
保存了我的一天!
pendingIntent = PendingIntent.getActivity(
getContext(),
0,
new Intent(getContext(), getContext().getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);