Android how to keep your phone screen highlighted

  • 2020-06-23 01:53:38
  • OfStack

1 When I started to contact android application project, I was required to add a highlight when the project was about to go online. Every time I directly went to Baidu 1 on the Internet. Now I am playing the game, but it is still the same requirement

First, add the following code to Activity:

Member variables:


private PowerManager.WakeLock wakeLock = null; 
 Rewrite the parent class Activity Two methods and add 1 The logic: 
@Override 
  public void onResume() {
    super.onResume();  
    acquireWakeLock(this); 
  } 
  @Override 
  public void onPause() { 
    super.onPause(); 
    releaseWakeLock(); 
  } 
  public void acquireWakeLock(Context context) { 
    if (wakeLock == null) { 
      PowerManager powerManager = (PowerManager) (context 
          .getSystemService(Context.POWER_SERVICE)); 
      wakeLock = powerManager.newWakeLock( 
          PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); 
      wakeLock.acquire(); 
    } 
  } 
  public void releaseWakeLock() { 
    if (wakeLock != null && wakeLock.isHeld()) { 
      wakeLock.release(); 
      wakeLock = null; 
    } 
  }

Then, add the following permissions in AndroidManifest. xml:


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

Related articles: