我试图让我的应用程序在运行时忽略 nfc 命令 - 它是由带有 Android 应用程序记录 (AAR) 的 NFC 标签启动的,我不希望它在已经运行时能够通过该标签启动。尝试过小心翼翼地跟随别人examples https://stackoverflow.com/questions/9748513/reading-nfc-tags-only-from-a-particuar-activity,但应用程序仍然可以在运行时(在前台)由 AAR 启动。
清单.xml:
<application>
<activity>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/com.MyApp.frontcam" />
</intent-filter>
</activity>
</application>
活动.java:
private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
@Override
public void onCreate(Bundle savedInstanceState) {
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("*/*");
}
catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mFilters = new IntentFilter[] {ndef, };
mTechLists = new String[][] {
new String[] { NfcA.class.getName() },
new String[] { Ndef.class.getName() },
new String[] { NdefFormatable.class.getName() }
};
}
@Override
protected void onResume() {
super.onResume();
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); //ended up setting mFilters and mTechLists to null
}
您可以通过设置启动模式来实现此目的:
<application>
<activity android:launchmode="singleTask">
...
单一任务:系统在新任务的根部创建活动并将意图路由到该活动。但是,如果该活动的实例已存在,系统将通过调用其 onNewIntent() 方法将 Intent 路由到现有实例,而不是创建一个新实例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)