当设备处于睡眠模式可以AlarmManager.setInexactRepeating不广播意图? [英] Could AlarmManager.setInexactRepeating not broadcast intents when the device is in sleep mode?

查看:720
本文介绍了当设备处于睡眠模式可以AlarmManager.setInexactRepeating不广播意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的类,设置和取消的AlarmManager广播意图:

I have a very simple class that sets and cancels an AlarmManager to broadcast intents:

public class MyIntentsAlarm {

 public void setAlarm(Context context){
  AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  Intent i = new Intent("MY_ACTION");
  PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
  am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),5*60*1000L, pi);
 }

 public void cancelAlarm(Context context){
  Intent intent = new Intent("MY_ACTION");
  PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
  AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  alarmManager.cancel(sender);
 }
}

我把我的活性及的onCreate()方法 setAlarm(上下文的背景下)方法 cancelAlarm(上下文的背景下)的onDestroy()。然后在我的活动的的onCreate()方法我注册一个BroadcastReceiver来听这个AlarmManager广播的意图和执行某些任务(在获取唤醒锁来保证任务执行):

I call the setAlarm(Context context) method in the onCreate() method of my Activity and the cancelAlarm(Context context) in the onDestroy(). Then in the onCreate() method of my Activity I register a BroadcastReceiver to listen to the intents broadcasted by this AlarmManager and perform some task (while acquiring a wake lock to guarantee that the tasks are performed):

myActionAlarmReceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
  Log.d(TAG, "MY_ACTION intent received");
  PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
  wl.acquire();
  runMyTask();
  wl.release();
 }
};
registerReceiver(myActionAlarmReceiver, new IntentFilter("MY_ACTION"));

是否有可能,当手机处于睡眠模式我AlarmManager不播意图,即使我使用 RTC_WAKEUP 键入和我取得激活锁定<我的广播接收器/ code>

Is it possible that my AlarmManager does not broadcast intents when the phone is in sleep mode, even if I use the RTC_WAKEUP type and I acquire a WakeLock in my BroadcastReceiver?

我的经验,到目前为止,在多台设备复制,就是意图将播出的第一个晚上,我的设备处于睡眠模式(我的应用程序在一天中不断磨合),但后来,它从来没有在晚上播放意图试。

My experience so far, reproduced in multiple devices, is that the intents will be broadcasted the first night that my device is in sleep mode (my app is constantly running during the day), but then, it never broadcasts intents at night again.

任何想法?

推荐答案

试试这个

     SystemClock.elapsedRealtime()

    System.currentTimeMillis()

这篇关于当设备处于睡眠模式可以AlarmManager.setInexactRepeating不广播意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆