How to disable default Android NFC app when NFC is used in my app [duplicate]
Asked Answered
A

3

4

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.

How can I disable the Android app?

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);
}
Aplanatic answered 13/9, 2014 at 16:21 Comment(1)
Have you tried calling setupForegroundDispatch() as soon as your app is running? (in method onResume() for example)Monniemono
B
8

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.

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.

For example:

  • 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!

Banlieue answered 14/9, 2014 at 16:36 Comment(0)
S
1

I think I figured out a simple solution which is when you create your PendingIntent, for the 3rd argument simply put new Intent(). So:

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

And then set your NFC adapter as:

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

Do this in all of the activities in which you don't want anything to happen in your own app when you scan an NFC tag (though it will create a sound), and you also don't want the default Android NFC reader to pop up.

Stampede answered 28/7, 2019 at 1:0 Comment(0)
C
0

I was getting the same problem, in my phone there was 2 nfc enabled app installed. Whenever i start my app instantly default app opens, for this i create a BaseActivity and extend it in my all activities and there i overrided both methods

adapter.disableForegroundDispatch(mainActivity);
enableForegroundDispatch()

and i have onNewIntent(Intent intent) method only interested activity and i have a check like

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

so i get rid of opening of default app when my app running.

Note But i have still one issue sometimes still opening other app when my app running. Please genius look into this :-)

Congruous answered 11/5, 2016 at 11:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.