##利用广播监听手机解锁、锁屏和亮屏的操作
public class MainActivity extends Activity {
BroadcastReceiver receiver;
private static String TAG = "MyLog";
private ScreenListener MyListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestBroadReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
registerReceiver(receiver, filter);
}
public void TestBroadReceiver() {
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_SCREEN_ON.equals(action)) {
Log.i(TAG, "开屏");
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Log.i(TAG, "锁屏");
} else if (Intent.ACTION_USER_PRESENT.equals(action)) {
Log.i(TAG, "解锁");
}
}
};
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}