Android: How to turn screen on and off programmatically?

后端 未结 16 2059
醉酒成梦
醉酒成梦 2020-11-22 13:05

Before marking this post as a \"duplicate\", I am writing this post because no other post holds the solution to the problem.

I am trying to turn off the device, then

相关标签:
16条回答
  • 2020-11-22 13:34

    I have tried all above solution but none worked for me. so I googled and found below solutions. I tried and it worked.

    https://www.tutorialspoint.com/how-to-turn-android-device-screen-on-and-off-programmatically

    if there is any suggestion then please give

    0 讨论(0)
  • 2020-11-22 13:35

    Hi I hope this will help:

     private PowerManager mPowerManager;
     private PowerManager.WakeLock mWakeLock;
    
     public void turnOnScreen(){
         // turn on screen
         Log.v("ProximityActivity", "ON!");
         mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
         mWakeLock.acquire();
    }
    
     @TargetApi(21) //Suppress lint error for PROXIMITY_SCREEN_OFF_WAKE_LOCK
     public void turnOffScreen(){
         // turn off screen
         Log.v("ProximityActivity", "OFF!");
         mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "tag");
         mWakeLock.acquire();
    }
    
    0 讨论(0)
  • 2020-11-22 13:36
         WakeLock screenLock =    ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(
        PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        screenLock.acquire();
    
      //later
      screenLock.release();
    

    //User Manifest file

    0 讨论(0)
  • 2020-11-22 13:37

    I would suggest this one:

    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
    wl.acquire();
    

    The flag ACQUIRE_CAUSES_WAKEUP is explained like that:

    Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.

    Also, make sure you have the following permission in the AndroidManifewst.xml file:

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    0 讨论(0)
  • 2020-11-22 13:37

    Regarding to Android documentation it can be achieve by using following code line:

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

    I have added this in my onCreate method and it works fine.

    On the link you will find different ways to achieve this and general explanation as well.

    Link to the documenation: https://developer.android.com/training/scheduling/wakelock.html

    0 讨论(0)
  • 2020-11-22 13:40

    The best way to do it ( using rooted devices) :

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        .
        .
        .
    
        int flags = WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
        getWindow().addFlags(flags); // this is how your app will wake up the screen
        //you will call this activity later
    
       .
       .
       .
    }
    

    Now we have this two functions:

    private void turnOffScreen(){
    
      try{
         Class c = Class.forName("android.os.PowerManager");
         PowerManager  mPowerManager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
         for(Method m : c.getDeclaredMethods()){
            if(m.getName().equals("goToSleep")){
              m.setAccessible(true);
              if(m.getParameterTypes().length == 1){
                m.invoke(mPowerManager,SystemClock.uptimeMillis()-2);
              }
            }
         } 
      } catch (Exception e){
      }
    }
    

    And this:

    public void turnOnScreen(){
      Intent i = new Intent(this,YOURACTIVITYWITHFLAGS.class);
      startActivity(i);
    }
    

    Sorry for my bad english.

    0 讨论(0)
提交回复
热议问题