1

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.

Ran Zhang
  • 11
  • 3

1 Answers1

0

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • So my requirement is to write a kiosk app, which is why I implement the above code. But I also want to allow users to use as a normal device if they leave the app. Therefore it brings to this question. The BOOT_COMPLETE will only be triggered on broadcast Receiver and causing system stay on default home page for a few seconds which is something I don't want. The only possible solution I could think of is to find a way run default home screen at background but haven' had any useful solution. Do you have any idea for this? – Ran Zhang Dec 10 '21 at 00:46
  • Is it a requirement that your app run as soon as the phone starts? If the answer to the previous question is no then consider using ***Lock Task Mode*** in lieu of your current implementation to create a kiosk app that allows the user to lock into the app and exit on certain conditions. Code example: [start lock task mode](https://stackoverflow.com/questions/45247919/activity-startlocktask-from-one-activity-to-another-activity) – Alias Cartellano Dec 10 '21 at 21:56
  • @RanZhang does my comment work for you? – Alias Cartellano Dec 11 '21 at 02:18
  • Sorry the answer is yes. I need to prevent user to see any another screen before my app starts running. – Ran Zhang Dec 11 '21 at 07:31