Disable Screen Lock(Power) Button in Android

别来无恙 提交于 2019-11-27 22:27:20

问题


I want that when my application is running the power button (which upon pressing locks the screen & screen goes BLACK), should be disabled. So that the user cannot lock the screen.

I have noticed this thing in Samsung Galaxy S phone's Default Camera App. That's the same reason I am trying to do the same. I have also a Camera related App.


回答1:


try this one

int val=android.provider.Settings.System.getInt(getContentResolver(),
                                                                SCREEN_OFF_TIMEOUT);

                    android.provider.Settings.System.putInt(getContentResolver(),
                                                            SCREEN_OFF_TIMEOUT, -1);
                    Toast.makeText(this, "Disabled Screen Timeout", Toast.LENGTH_LONG).show();
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putInt("ScreenTimeout",val);
                    editor.commit();
                }
            } catch(Throwable er) {
                Toast.makeText(this, "Error "+er.getMessage(), Toast.LENGTH_LONG).show();
            } 

that will set screen off

to disable key guard in android use

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

and use permition

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

to keep screen alive

 @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }



回答2:


I have reached this using root access.

sqlite3 /data/data/com.android.providers.settings/databases/settings.db
insert into secure (name, value) values ('lockscreen.disabled', 1);

or depending on version

sqlite3 /data/system/locksettings.db
insert into locksettings (name, value) values ('lockscreen.disabled', 1);


来源:https://stackoverflow.com/questions/10817367/disable-screen-lockpower-button-in-android

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