在Android的唤醒/休眠活动启动 [英] Start Activity on wake up/sleep in Android
本文介绍了在Android的唤醒/休眠活动启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让它启动时,Android设备被唤醒计数,当Android设备被设置为休眠停止一个定时器。我什么也没找到,一个活动可能如何被触发
通过唤醒/休眠。
我希望你能帮助我与我的问题。
解决方案
我用一个BroadcastReceiver像timonvlad说,但ACTION_SCREEN_ON和ACTION_SCREEN_OFF不能用XML被称为
所以我创建了一个服务。该服务在XML中进行注册,然后我用这个code
OnOffReceiver延伸服务{public类@覆盖
公众的IBinder onBind(意向为arg0){
// TODO自动生成方法存根
返回null;
}@覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
registerReceiver(新广播接收器(){ 公共无效的onReceive(上下文的背景下,意图意图){
//这发生在屏幕关闭
}
},新的IntentFilter(Intent.ACTION_SCREEN_OFF));
registerReceiver(新广播接收器(){ 公共无效的onReceive(上下文的背景下,意图意图){
//出现这种情况,当屏幕开启和屏幕锁停用
}
},新的IntentFilter(Intent.ACTION_USER_ preSENT)); 返回START_STICKY;
}@覆盖
公共无效的onDestroy(){
super.onDestroy();
}
}
I want to make an timer which starts counting when the Android device is woken up and stops when the Android device is set to sleep. I found nothing, how a activity could be triggered by wake up/sleep.
I hope you can help me with my problem
解决方案
I used a BroadcastReceiver like timonvlad said but ACTION_SCREEN_ON and ACTION_SCREEN_OFF could not be called by XML so I created an service. The service has to be registered in the XML then I used this code
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();
}
}
这篇关于在Android的唤醒/休眠活动启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文