skip to Main Content

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


  1. Chosen as BEST ANSWER

    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

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application ... />
        <receiver
            android:name=".BootCompletedReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
    

    Adding following code in BootCompletedReceiver.class

    public class BootCompletedReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
                Toast.makeText(context, "DEVICE START-UP COMPLETED.", Toast.LENGTH_LONG).show();
    
                // START Settings app
                Intent settingsIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
                settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(settingsIntent);
    
                // START MainActivity
                Intent startAppIntent = new Intent(context, MainActivity.class);
                startAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(startAppIntent);
    
            }
        }
    }
    

  2. 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,

    @Override
    protected void onResume() {
        super.onResume();
    
        // Reinitialize NFCAdapter
        initializeNfcAdapter();
    }
    
    @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);
    }
    
    private void initializeNfcAdapter() {
        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);
        } else {
            // Handle the case where NFC is not supported on this device
            Toast.makeText(this, "NFC is not supported on this device.", Toast.LENGTH_LONG).show();
        }
    }
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search