【问题标题】:How to disable default Android NFC app when NFC is used in my app [duplicate]在我的应用程序中使用 NFC 时如何禁用默认的 Android NFC 应用程序 [重复]
【发布时间】:2014-09-13 16:21:26
【问题描述】:

在我的应用程序中,我使用 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() 中)

标签: java android nfc


【解决方案1】:

您无法禁用此行为。另一个应用程序启动,因为您的应用程序没有捕获标签。当您删除应用程序时,这将停止,但这当然不是解决方案。

如果您想防止这种情况发生,您应该在您的应用中捕获扫描的标签,该标签位于前台。
您已经知道如何使用enableForegroundDispatch 执行此操作。扫描标签时不要禁用前台调度,而是创建一个标志或其他东西来确定您是否要对标签执行某些操作。

例如:

  • 创建一个属性,例如doSomethingWithTheTag
  • 向您的标签处理程序添加一个条件,即doSomethingWithTheTag 应为true。如果是false,请不要对标签做任何事情。在大多数情况下,这将是您的 onNewIntent 覆盖,只需确保每个活动都覆盖该功能。
  • 当您的对话框打开时,将doSomethingWithTheTag 设置为true
  • 读取标签并处理
  • doSomethingWithTheTag 设置为false
  • 如果您现在扫描标签,它会被您的应用捕获,但不会发生任何事情。

希望我很清楚。祝你好运!

【讨论】:

    【解决方案2】:

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

    adapter.disableForegroundDispatch(mainActivity);
    enableForegroundDispatch()
    

    我有 onNewIntent(Intent intent) 方法只感兴趣的活动,我有一个类似的检查

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

    所以我在我的应用程序运行时摆脱了默认应用程序的打开。

    注意但我仍然有一个问题,有时在我的应用运行时仍会打开其他应用。请天才看看这个:-)

    【讨论】:

      【解决方案3】:

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

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

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

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

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

      【讨论】:

      • 为什么投反对票?
      猜你喜欢
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多