在我的应用程序中使用NFC时如何禁用默认的Android NFC应用程序 [英] How to disable default Android NFC app when NFC is used in my app

查看:265
本文介绍了在我的应用程序中使用NFC时如何禁用默认的Android NFC应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我正在使用NFC读取标签.我单击按钮以启用NFC.将打开一个进度对话框以读取NFC标记,完成后将禁用NFC.一切都很好.但是,当应用程序中未启用NFC且我在手机上放置了NFC标签时,默认的Android应用程序会读取NFC标签并将我的应用程序置于后台.

In my app I am using NFC to read tags. I click on the button to enable NFC. A progress dialog is opened to read the NFC tag, and when done, NFC is disabled. That's all working fine. But when NFC is not enabled in the app and I put a NFC tag to the phone, the default Android app reads the NFC tag and puts my app to the background.

如何禁用Android应用程序?

How can I disable the Android app?

我用于启用/禁用NFC的代码:

My code for enabling / disabling 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);
}

推荐答案

您不能禁用此行为.已启动另一个应用程序,因为您的应用程序未捕获该标签.当您删除应用程序时,该操作将停止,但这当然不是解决方案.

You can't disable this behavior. Another app is launched because your app doesn't catch the tag. When you delete the app this will stop, but ofcourse that's not the solution.

如果要防止这种情况,则应在应用程序中捕获已扫描的标签,该标签位于前台.
您已经知道如何使用enableForegroundDispatch执行此操作.扫描标签时,请不要禁用前台调度,而要创建一个标志或确定您是否要对该标签执行操作的操作.

If you want to prevent this you should catch the scanned tag in your app, which is on the foreground.
You already know how to do this with enableForegroundDispatch. Don't disable the foreground dispatch when a tag is scanned, but create a flag or something which determines if you want to do something with the tag or not.

例如:

  • 创建一个属性,例如doSomethingWithTheTag
  • 向标签处理程序添加一个条件,即doSomethingWithTheTag应该为true.如果是false,请不要对标签执行任何操作.在大多数情况下,这将是您的onNewIntent覆盖,只需确保每个活动都覆盖该功能即可.
  • 打开对话框时,将doSomethingWithTheTag设置为true
  • 读取标签并对其进行处理
  • doSomethingWithTheTag设置为false
  • 如果您现在扫描标签,则该标签将被您的应用程序捕获,但是什么也不会发生.
  • Create an attribute e.g. doSomethingWithTheTag
  • Add a condition to your tag handler that doSomethingWithTheTag should be true. If it's false don't do something with the tag. In most cases, this will be your onNewIntent override, just make sure every activity overrides that function.
  • When your dialog opens set the doSomethingWithTheTag to true
  • Read the tag and handle it
  • Set doSomethingWithTheTag to false
  • If you scan a tag now, it will be catched by your app, but nothing will happen.

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

Hope i'm clear. Good luck!

这篇关于在我的应用程序中使用NFC时如何禁用默认的Android NFC应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆