Disable Screen Lock(Power) Button in Android

后端 未结 2 1580
悲哀的现实
悲哀的现实 2020-12-31 23:33

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

2条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 23:42

    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

    
    

    to keep screen alive

     @Override
        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);
    
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    

提交回复
热议问题