Disable power button… or… Resume full screen in Android toddler app

℡╲_俬逩灬. 提交于 2019-12-04 14:14:10

If the question is boolean : Is it possible to override POWER button? The answer is YES. Please remember, I am not saying anything about the context in which such a behavior should or should not be chosen or will the situation benefit you in what you are trying to achieve!

OVERRIDING POWER BUTTON IS POSSIBLE on non-rooted phones(atleast 2.3.5 and 2.3.6)! If you dont believe me then just download Theft Aware free app from Google play(version before it got acquired by AVAST) and check it out yourself. I have tested it on non-rooted Samsung Galaxy Y (android 2.3.6) and HTC Wildfire-S (android 2.3.5).

I AM NOT saying its possible using the publicly documented android APIs but it IS possible on a non-rooted factory android firmware(2.3.5 and 2.3.6 atleast if not others). May be Theft Aware guys might have hooked on to the OS level/Reverse Engineered it or have dug deep into the source code and might have figured out some way of overriding the power button but they did it absolutely brilliantly.

This is their IP and that is the reason there is no document, or paper or forum discussion solution about how they did it. I have been trying to figure it out myself for last 2 weeks. Not successful yet, but if they can do it...means, it can be DONE!

Got tired of reading that it cannot be done, hence the answer. Keep Looking and if someone finds the trick please post it!

P.S : I am not saying its a good thing to do. I am not saying that you should disable power button and end up with a bad user experience app. The answer just STATES that overriding the POWER button IS POSSIBLE!

OK, after several google searches and trial/error troubleshooting I finally figured out the answer to this problem. If you're trying to make an Android app that locks all the buttons so toddlers cannot access other areas of the device, do the following...

In the manifest, add the following to your activity. This causes your activity to display full screen:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

Also in the manifest, add the following line. This allows your activity to control whether or not the lock screen appears when the device's screen is turned off and then back on:

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

In your activity, import the following. This gives you access to the keygaurd manager so you can prevent the lock screen from appearing:

import android.app.KeyguardManager;

In your activity's class definition, add the following two variables. These allow the keygaurd manager to be used by the onCreate(...), onPause(...) and onResume(...) events (see below).

KeyguardManager keyguardManager;
KeyguardManager.KeyguardLock lock;

In your activity's onCreate(...) event, add the following code. This assigned the keylock controls to your class variables defined above and prevents the lock screen from appearing if the screen is turned off and then back on (power button is pressed):

keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();

In your activity's onPause(...) event, add the following code. This re-enables the keygaurd lock if your activity looses focus, for example when the user enters the correct code to exit the locked app (like pressing the four corners in a clockwise motion).

lock.reenableKeyguard();

In your activity's onResume(...) event, add the folling code. This disables the key lock screen once again when your app resumes from the screen being turned off.

lock.disableKeyguard();

That's all there is to it.

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