I am using NFC tag read in my application, which is working 100% ok. On certain button click we read NFC tag and handle it accordingly
The problem is if user put NFC tag while anywhere in the application it auto triggers the third party application/default application which I don't want NFC hadnler should only open if intentionally by code not automatically
What I want is if user is in my application NFC read should only work on button click not anonymous, one button click all works fine but anonymous tag reading is creating problem
Platform: Android Device: Samsung Galaxy
Is there anything we can handle from code?
I am using NFC tag read in my application, which is working 100% ok. On certain button click we read NFC tag and handle it accordingly
The problem is if user put NFC tag while anywhere in the application it auto triggers the third party application/default application which I don't want NFC hadnler should only open if intentionally by code not automatically
What I want is if user is in my application NFC read should only work on button click not anonymous, one button click all works fine but anonymous tag reading is creating problem
Platform: Android Device: Samsung Galaxy
Is there anything we can handle from code?
Share Improve this question asked 2 days ago Tarun SeeraTarun Seera 4,4124 gold badges29 silver badges43 bronze badges1 Answer
Reset to default 1Yes! You can control NFC behavior in your Android application to prevent it from being handled by third-party apps unless triggered explicitly within your app.
Solution: Disable Foreground Dispatch When Not Needed By default, NFC events are handled by the Android system and may trigger third-party apps. To prevent this, you should enable NFC foreground dispatch only when necessary (e.g., on button click) and disable it otherwise.
Steps to Achieve This: Enable Foreground Dispatch on Button Click Disable Foreground Dispatch When Not Needed Use a Foreground Activity to Prioritize Your App
- Setup Foreground Dispatch in Your Activity
@Override
protected void onResume() {
super.onResume();
enableForegroundDispatch();
}
@Override
protected void onPause() {
super.onPause();
disableForegroundDispatch();
}
- Enable Foreground Dispatch Only on Button Click
private void enableForegroundDispatch() {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
if (adapter != null) {
Intent intent = new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE);
IntentFilter[] filters = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED)};
String[][] techLists = new String[][]{};
adapter.enableForegroundDispatch(this, pendingIntent, filters, techLists);
}
}
- Disable Foreground Dispatch When Not Needed
private void disableForegroundDispatch() {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
if (adapter != null) {
adapter.disableForegroundDispatch(this);
}
}
- Handle NFC Events Only When Needed
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (isNfcReadingEnabled) { // Only process NFC when enabled
processNfcTag(intent);
}
}
- Control NFC Read with a Button Click
private boolean isNfcReadingEnabled = false;
public void onNfcButtonClick(View view) {
isNfcReadingEnabled = true;
enableForegroundDispatch();
Toast.makeText(this, "NFC Ready", Toast.LENGTH_SHORT).show();
}
- Ensure your AndroidManifest.xml includes NFC permissions:
<uses-permission android:name="android.permission.NFC"/>
<uses-feature android:name="android.hardware.nfc" android:required="true"/>