I have an Android 8.1 tablet where I’ve developed a Kiosk application. Currently, I’m facing an issue where onNewIntent()
doesn’t work after a system reboot.
When the system reboots, I run a command like adb shell am start com.android.settings
and then return to my application, or close my app onNewIntent()
starts working again.
When I reinstall the application using Android Studio, onNewIntent()
works.
So, the problem only occurs after the system is rebooted, onNewIntent()
doesn’t discover NFC tags anymore in my application (although the device still plays the sound of the NFC tag being detected).
Android code
@Override
protected void onResume() {
super.onResume();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter != null) {
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
}
@Override
protected void onPause() {
super.onPause();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter != null) nfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Toast.makeText(this, "NFC TAG DISCOVERED.", Toast.LENGTH_LONG).show();
// ...
}
setIntent(intent);
}
in case it’s required, this is how i turn my device into kiosk mode:
.adb -s 192.168.2.170:5555 shell dpm set-device-owner com.crezzur.dienstplannerprodvx/.DeviceOwnerReceiver
private void setupDeviceOwner(Context context) {
DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName adminComponentName = DeviceOwnerReceiver.getComponentName(this);
if (dpm.isDeviceOwnerApp(getPackageName())) {
//startLockTask();
dpm.setLockTaskPackages(adminComponentName, new String[]{
"com.crezzur.dienstplannerprodvx",
"com.android.settings",
});
dpm.setKeyguardDisabled(adminComponentName, true);
dpm.setStatusBarDisabled(adminComponentName, true);
}
dpm.isDeviceOwnerApp(getPackageName());
}
2
Answers
I have found a solution, but it's more of a "messy" solution. If anyone has a better suggestion, I'm definitely willing to consider it.
My messy solution is to listen for RECEIVE_BOOT_COMPLETED and then immediately open the settings app followed by my application. This way, the NfcAdapter will still works after a system reboot without any manual intervention.
Adding following code in AndroidManifest.xml
Adding following code in BootCompletedReceiver.class
It might be an issue with the initialization of the NFCAdapter after a system reboot. To address this, you can try reinitializing the NFCAdapter in the onResume() method. Additionally, make sure to request NFC permissions in your AndroidManifest.xml file. Try the below code,
Make sure to call initializeNfcAdapter() in both onResume() and after the system reboot. If this doesn’t solve the issue, you may need to investigate further and check if there are any specific conditions related to the system reboot that affect the NFC functionality on your device.