Java 在我的应用程序中使用NFC时,如何禁用默认Android NFC应用程序
在我的应用程序中,我使用NFC读取标签。我点击按钮启用NFC。打开进度对话框以读取NFC标记,完成后,NFC被禁用。一切都很好。但当应用程序中未启用NFC时,我将NFC标签放在手机上,默认的Android应用程序会读取NFC标签并将我的应用程序放在后台 如何禁用Android应用程序 启用/禁用NFC的我的代码:Java 在我的应用程序中使用NFC时,如何禁用默认Android NFC应用程序,java,android,nfc,Java,Android,Nfc,在我的应用程序中,我使用NFC读取标签。我点击按钮启用NFC。打开进度对话框以读取NFC标记,完成后,NFC被禁用。一切都很好。但当应用程序中未启用NFC时,我将NFC标签放在手机上,默认的Android应用程序会读取NFC标签并将我的应用程序放在后台 如何禁用Android应用程序 启用/禁用NFC的我的代码: /** * @param activity The corresponding {@link Activity} requesting the foreground dispatch
/**
* @param activity The corresponding {@link Activity} requesting the foreground dispatch.
* @param adapter The {@link NfcAdapter} used for the foreground dispatch.
*/
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[1];
String[][] techList = new String[][]{};
// Notice that this is the same filter as in our manifest.
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
try {
filters[0].addDataType(MIME_TEXT_PLAIN);
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException(activity.getString(R.string.exception_wrong_mime_type));
}
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
/**
* @param activity The corresponding {@link MainActivity} requesting to stop the foreground dispatch.
* @param adapter The {@link NfcAdapter} used for the foreground dispatch.
*/
public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
adapter.disableForegroundDispatch(activity);
}
无法禁用此行为。另一个应用程序已启动,因为您的应用程序未捕获标记。当你删除应用程序时,这将停止,但这当然不是解决方案 如果您想防止这种情况发生,您应该在应用程序中捕获扫描的标签,该标签位于前台。
您已经知道如何使用
enableForegroundDispatch
执行此操作。扫描标记时,不要禁用前台调度,而是创建一个标志或决定是否要对标记执行操作的内容
例如:
- 创建一个属性,例如
doSomethingWithTag
- 在标记处理程序中添加一个条件,即
应为doSomethingWithTag
。如果是true
,请不要对标记执行任何操作。在大多数情况下,这将是您的false
覆盖,只需确保每个活动都覆盖该函数即可onNewIntent
- 当对话框打开时,将带有标签的
dosomething设置为
true
- 阅读标签并处理它
- 将带有标签的
dosomething设置为
false
- 如果你现在扫描一个标签,它会被你的应用程序捕获,但不会发生任何事情
希望我明白了。祝你好运 我也遇到了同样的问题,我的手机上安装了两个支持nfc的应用程序。每当我立即启动我的应用程序时,默认应用程序就会打开,为此我创建了一个BaseActivity并在我的所有活动中扩展它,在那里我覆盖了这两个方法
adapter.disableForegroundDispatch(mainActivity);
enableForegroundDispatch()
我有一个onNewIntent(Intent-Intent)方法,只有感兴趣的活动,我有一个
@Override
protected void onNewIntent(Intent intent)
{
if (!isThisDialogWindow)
handleIntent(intent);
}
因此,当我的应用程序运行时,我不再打开默认应用程序
注意但我仍然有一个问题,有时在我的应用程序运行时仍然打开其他应用程序。请天才们研究一下:-)我想我找到了一个简单的解决方案,就是当你创建你的
pendingent
时,对于第三个参数,只需放入new Intent()
。因此:
if (pendingIntent == null) {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
}
然后将NFC适配器设置为:
adapter.enableForegroundDispatch(this,pendingent,null,null)代码>
当你扫描NFC标签时,如果你不希望自己的应用程序发生任何事情(尽管它会发出声音),那么在所有活动中都要这样做,而且你也不希望默认的Android NFC阅读器弹出 您是否尝试过在应用程序运行后立即调用setupForegroundDispatch()
?(例如,在方法onResume()
中)为什么否决?