你需要做到以下幾點:
創建一個BroadcastReceiver
public class ScreenStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_SCREEN_TURNED_OFF.equals(action)) {
// Screen is off
} else if (Intent.ACTION_USER_PRESENT.equals(action)) {
// Screen is on
}
}
}
註冊或者是通過代碼或在清單
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
context.registerReceiver(mScreenReceiver, filter, null, null);
,基本上你都設置做你的事。
嗯。很好!但是如何插入一個秒錶代碼和你給出的代碼。 HM。 –
當屏幕開啓時開始觀看,當屏幕關閉時將其停止 - 將其放入數據庫。 – MarkySmarky