简体   繁体   中英

Turning screen on and off in Android

I am having a bit of trouble with regards to the Android code I am trying to produce. What I am trying to do is simply print out a 1 if the user wants to unlock their phone, and when the program first starts, and a 0 when the user presses the screen lock to lock their phone. I thought this had to do with the Android lifecycle so I tried using onPause and onResume, but my program only prints out 0's, never 1's. The logTime method simply prints out the 1 or a 0, and the onCreate method in the MainActivity calls onResume() once. Here is my code:

MainActivity:

protected void onPause(){
    if(ScreenReceiver.screenOn){
        logTime(ScreenReceiver.screenOn); 
 super.onPause();
}

protected void onResume()
  if(!ScreenReceiver.screenOn){
    logTime(!ScreenReceiver.screenOn);
super.onResume();
}

Screen Receiver:

public class ScreenReceiver extends BroadcastReceiver{
public static boolean screenOn;


@Override
public void onReceive(Context context, Intent intent){
    if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
        screenOn = false;
    }
    else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
        screenOn = true;
    }

}

}

I'm not sure why it only prints out 0's. Might anyone know why? Thanks!

Everything seems right based on your code, it should always print 0...

When the screen is turned off, screenOn is set to false , your Activity's onPause is called, it prints 0.

When your application is running and onResume is called, screenOn would be true (whether the screen was just turned on or not), there, you log the opposite of screenOn , so would be 0 again...

There is a delay between your Activity's onPause() and the actual Broadcast being received, so usually you can get "odd" (wrong) readings since the variable change hasn't happened. This receiver needs to be dynamically registered, and you also want it to receive even when the screen is off and turns back on. Usually, Receivers are unregistered in onPause() to avoid leaking, but here, we're going to use onDestroy() since that's the last method call we can use. For simplicity, I made the BroadcastReceiver right in the Activity and called logTime() outright.

Sample Code:

public class MainActivity extends Activity {

  BroadcastReceiver receiver;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
    filter.addAction(Intent.ACTION_SCREEN_OFF); 
    receiver = new BroadcastReceiver(){

      @Override
      public void onReceive(Context arg0, Intent arg1) {
        if(arg1.getAction().equals(Intent.ACTION_SCREEN_OFF)){
          logTime(false);
        }
        else if(arg1.getAction().equals(Intent.ACTION_SCREEN_ON)){
          logTime(true);
        }
      } 
    }; 
    registerReceiver(receiver, filter);
  }

  @Override
  protected void onDestroy()
  {
    try{
      unregisterReceiver (receiver);
    }
    catch (NullPointerException e){  
      e.printStackTrace();
    }
    catch (IllegalStateException e1){
      e.printStackTrace();
    }
    super.onDestroy();
  }


  public void logTime (boolean screen)
  {
    Time now = new Time();
    now.setToNow();

    String lsNow = now.format("%m-%d-%Y %I:%M:%S");
    TextView myText = (TextView) findViewById (R.id.myText);
    myText.append (" " + lsNow + (screen ? "0" : "1"));
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM