微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在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();
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐