Skip to content

Instantly share code, notes, and snippets.

@sheng168
Created May 2, 2013 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheng168/5502894 to your computer and use it in GitHub Desktop.
Save sheng168/5502894 to your computer and use it in GitHub Desktop.
android detect screen on/on
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class ScreenReceiver extends BroadcastReceiver {
public static boolean screenOff;
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive ");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
System.out.println("SCREEN TURNED OFF on BroadcastReceiver");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = true;
System.out.println("SCREEN TURNED ON on BroadcastReceiver");
} else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
screenOff = false;
System.out.println("ACTION_USER_PRESENT on BroadcastReceiver");
}
stateChange();
}
public void register(Context ctx) {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
// BroadcastReceiver mReceiver = new ScreenReceiver();
ctx.registerReceiver(this, filter);
}
protected void stateChange() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment