如何在我的应用程序中使用NFC时禁用默认的Android NFC应用程序

4

在我的应用程序中,我使用NFC读取标签。我点击按钮启用NFC。打开进度对话框以读取NFC标签,完成后禁用NFC。这一切都很好运作。但是,当应用程序中未启用NFC并且我将NFC标签放到手机上时,系统默认的Android应用程序读取了NFC标签,并将我的应用程序置于后台。

我如何禁用Android应用程序?

以下是我用于启用/禁用NFC的代码:

/**
 * @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);
}

你尝试过在应用程序启动时立即调用setupForegroundDispatch()吗?(例如在onResume()方法中) - mictter
3个回答

8
您无法禁用此行为。由于您的应用程序未捕获标签,因此会启动另一个应用程序。当您删除应用程序时,这将停止,但这显然不是解决方案。
如果要防止这种情况发生,您应该在前台的应用程序中捕获扫描的标签。您已经知道如何使用enableForegroundDispatch来做到这一点。不要在扫描到标签时禁用前台分派,而是创建一个标志或其他内容,以确定是否要对标签执行某些操作。
例如:
  • 创建一个属性,例如doSomethingWithTheTag
  • 向您的标签处理程序添加一个条件,即doSomethingWithTheTag应为true。如果它是false,则不要对标签执行任何操作。在大多数情况下,这将是您的onNewIntent重写函数,只需确保每个活动都重写该函数。
  • 打开对话框时将doSomethingWithTheTag设置为true
  • 读取标签并处理它
  • doSomethingWithTheTag设置为false
  • 现在,如果您扫描标签,它将被您的应用程序捕获,但不会发生任何事情。
希望我表达清楚了。祝好运!

1

我想我找到了一个简单的解决方案,就是当您创建 PendingIntent 时,对于第三个参数只需输入 new Intent()。所以:

    if (pendingIntent == null) {
        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
    }

然后将您的NFC适配器设置为:

adapter.enableForegroundDispatch(this, pendingIntent, null, null);

在所有活动中执行此操作,这些活动中您不希望在扫描NFC标签时发生任何事情(尽管它会发出声音),并且您也不希望默认的Android NFC阅读器弹出。


0

我遇到了同样的问题,在我的手机上安装了两个启用NFC的应用程序。每当我启动我的应用程序时,立即打开默认应用程序,因此我创建了一个BaseActivity并在所有活动中扩展它,在那里我覆盖了两种方法。

adapter.disableForegroundDispatch(mainActivity);
enableForegroundDispatch()

我有一个只对特定活动感兴趣的onNewIntent(Intent intent)方法,并且我有一个类似于以下的检查:

@Override
protected void onNewIntent(Intent intent)
{
    if (!isThisDialogWindow)
        handleIntent(intent);
}

我想在我的应用程序运行时取消默认应用程序的打开。

注意,但有时我的应用程序运行时仍会打开其他应用程序。请聪明的人看看这个问题 :-)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接