问题
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);
}
回答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 betrue
. If it'sfalse
don't do something with the tag. In most cases, this will be youronNewIntent
override, just make sure every activity overrides that function. - When your dialog opens set the
doSomethingWithTheTag
totrue
- Read the tag and handle it
- Set
doSomethingWithTheTag
tofalse
- If you scan a tag now, it will be catched by your app, but nothing will happen.
Hope i'm clear. Good luck!
回答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 :-)
回答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.
来源:https://stackoverflow.com/questions/25825188/how-to-disable-default-android-nfc-app-when-nfc-is-used-in-my-app