在Android中唤醒/睡眠时启动活动

我想制作一个计时器,当 Android设备被唤醒时开始计时,并在Android设备设置为休眠时停止计时.我什么都没发现,活动是如何被触发的
通过唤醒/睡眠.

我希望你能解决我的问题

解决方法

我使用了像timonvlad这样的broadcastReceiver,但是XML无法调用ACTION_SCREEN_ON和ACTION_SCREEN_OFF,所以我创建了一个服务.该服务必须在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();
}
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...