我想制作一个计时器,当 Android 设备被唤醒时开始计时,当 Android 设备设置为睡眠时停止计时。我什么也没找到,如何触发活动
通过唤醒/睡眠。
我希望你能帮助我解决我的问题
我使用了像 timonvlad 所说的 BroadcastReceiver,但是 ACTION_SCREEN_ON 和 ACTION_SCREEN_OFF 无法通过 XML 调用,所以我创建了一个服务。该服务必须在 XML 中注册,然后我使用了这段代码
public class OnOffReceiver extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
//This happens when the screen is switched off
}
}, new IntentFilter(Intent.ACTION_SCREEN_OFF));
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
//This happens when the screen is turned on and screen lock deactivated
}
}, new IntentFilter(Intent.ACTION_USER_PRESENT));
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)