Skip to content Skip to sidebar Skip to footer

How To Disable Default Android Nfc App When Nfc Is Used In My App

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

Solution 1:

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!

Solution 2:

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 :-)

Solution 3:

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.

Post a Comment for "How To Disable Default Android Nfc App When Nfc Is Used In My App"