Is there a way to automatically launch your app after the QR code provisioning is done?

106 Views Asked by At

I have an android app that is a device owner which I provision via QR code. I would like to launch the app automatically as soon as the provisioning setup is complete.

I tried to do it using the code below, which works for a normal restart of the device, but not for this particular case.

<receiver
    android:name=".receiver.BootCompleteReceiver"
    android:enabled="true"
    android:directBootAware="true"
    android:exported="true"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
        <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
    </intent-filter>
</receiver>
class BootCompleteReceiver: BroadcastReceiver() {
    @SuppressLint("UnsafeProtectedBroadcastReceiver")
    override fun onReceive(context: Context, intent: Intent?) {
        val startIntent = Intent(context, MainActivity::class.java)
        startIntent = Intent.FLAG_ACTIVITY_NEW_TASK
        context.startActivity(carlIntent)
    }
}

After the setup is complete, the receiver doesn't get any intent.

1

There are 1 best solutions below

0
Mohsen Abedini On

use onProfileProvisioningComplete() method in your DeviceAdminReceiver class.

this method trigger after success provision complete with Qr or Nfc

like this:

class DeviceAdminReceiver : DeviceAdminReceiver() {

    override fun onProfileProvisioningComplete(context: Context, intent: Intent) {
        super.onProfileProvisioningComplete(context, intent)
        Toast.makeText(context, "Profile Provisioning Completed", Toast.LENGTH_SHORT).show()
    }

    override fun onEnabled(context: Context, intent: Intent) {
        super.onEnabled(context, intent)
    }

    override fun onDisabled(context: Context, intent: Intent) {
        Toast.makeText(context, "onDisabled", Toast.LENGTH_SHORT).show()
    }
}

and in manifest file:

<receiver
            android:name=".core.broadcast.DeviceAdminReceiver"
            android:enabled="true"
            android:exported="true"
            android:label="device_admin"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin_receiver" />
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
                <action android:name="android.app.action.PROFILE_PROVISIONING_COMPLETE" />
                <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
                <action android:name="android.app.action.DEVICE_OWNER_CHANGED" />

            </intent-filter>
        </receiver>

remember dont remove super in onProfileProvisioningComplete method