Created
May 2, 2013 15:12
-
-
Save sheng168/5502894 to your computer and use it in GitHub Desktop.
android detect screen on/on
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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