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

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

I'm creating an Android app for toddlers. So, I need to lock as many buttons as possible to prevent the toddler from accessing other features of the Android device. Basically, I'm looking to reproduce the locking mechanism in popular toddler apps like "Toddler Lock". I have logic that requires the user to tap the four corners of the screen in a clockwise motion to exit the app.

To show the app in full-screen I have the following in my manifest

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

To show the app in landscape mode only, I have the following in my activity's onCreate event

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

To prevent any of the buttons from being used, I override the onKeyDown event with the following code

@Override public boolean onKeyDown(int keyCode, KeyEvent event) {     return true; } 

It all works great. So far, so good. There is only one button I can't seem to disable... the power button. I'm operating under the assumption that there is no way to disable the power button. Please let me know if I'm wrong. So, I need to figure out how to deal with that. Obviously, if the power button is held in and the device is powered down then there is nothing I can do about that. However, if the power button is tapped to turn the screen off and then tapped again to turn the screen back on, I would like to be able to deal with that appropriately.

What's happening is that the app resumes running, but it is no longer in full screen. The notification bar is displayed. As such, I can pull it down and access other areas of Android.

Why is it that waking the screen on the Android device causes the notification bar to appear over a full-screen app configured as described above... and how do I prevent it?

回答1:

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.



回答2:

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!



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