block Home Button in Ice cream sandwich and jelly bean

谁说我不能喝 提交于 2019-11-27 08:08:35
Swayam

Post ICS i.e. Android 4+, the overriding of the HomeButton has been removed for security reasons, to enable the user exit in case the application turns out to be a malware.

Plus, it is not a really good practice to not let the user navigate away from the application. But, since you are making a lock screen application, what you can do is declare the activity as a Launcher , so that when the HomeButton is pressed it will simply restart your application and remain there itself (the users would notice nothing but a slight flicker in the screen).

EDIT #1 : Here is another workaround, more suited to your needs.

EDIT #2 : Just came across this. Haven't tested it. But looks kinda promising. Not sure if it would work, but you could give it a try.

There are few things that you can try:

  • You can set your activity single top, and start it over with clear to top flag when onPause() method is called, this will block the home button and opening other activities.

  • Listen to BOOT_COMPLETED broadcast to start your activity - this will protect you from users who will take the battery out of the device in order to reboot it.

  • Add Alarmmanager that will test every second if your app is alive and if it is not, then start it - This will protect you from userers that some how managed to close your app(may be with external tools).

Do this and no one be able to exit your app.

I think it is impossible to detect and/or intercept the HOME button from within an Android app. This is built into the system to prevent malicious apps that cannot be exited.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!