Android 6.0.1 application auto start after boot

680 views Asked by At

I want to start my MainActivity right after device boot. I tried multiple solutions but no one works. Currently, I have this.

AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver android:name="installer.common.InstallerBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

InstallerBroadcastReceiver.kt

class InstallerBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val i = Intent(context, MainActivity::class.java)
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        i.putExtra("test", 1)
        context.startActivity(i)
    }
}

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main)
    if (intent.hasExtra("test")) {
        someMethodHere()
    }
}

Any suggestion on what can be wrong?

2

There are 2 answers

0
Coldnight On BEST ANSWER

Seems like there is some problem with device (zkteco), i get this error I/BackgroundManagerService: prevent from boot complete broadcast: com.mypackagename

On other devices i tried, it was working.

1
snachmsm On

try to split your intent-filter

    <intent-filter>
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>