Android Running app on reboot causing android default home screen not activate

Viewed 72

So I have an app and I want it to run immediately whenever device got rebooted. To achieve this I added home and default category to MainActivity.

<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />

However after doing this since default home screen is not launched it needs to click home button twice to go back to the home screen. I know that I could handle this by monitor home button click action like follow.

override fun onAttachedToWindow() {
    super.onAttachedToWindow()
    this.window.setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
}

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        //open defualt home screen intent
    }
    return super.onKeyDown(keyCode, event)
}

However, if user goes to somewhere else (let's say open setting from status bar) then click home button. This issue will happen again.

Therefore is there a way to avoid this? Maybe consider run default home screen at background when device get reboot but I honestly don't know the solution.

1 Answers

Setting yourself up as HOME means you're claiming to be a launcher app- a replacement for the homescreen. Having 2 launcher apps running at once confused the system. Don't do that. If you want to be launched on boot, use a BOOT_COMPLETE listener and launch an Activity from it. The method you used only should be used if you're writing an alternative home screen or a kiosk app.

Related