【问题标题】:Alarm Manager setRepeating stops working after a while警报管理器 setRepeating 一段时间后停止工作
【发布时间】:2016-07-30 16:16:36
【问题描述】:

我知道周围有类似的问题,但没有一个对我有帮助...我正在尝试设置一个重复警报来触发广播接收器,而这又会触发一个意图服务。我知道打瞌睡模式会推迟警报,我不介意。起初警报工作正常,但一段时间后它根本没有发出。这就是我设置闹钟的方式:

    public static void startRepeated(Context context){
        Intent intent=new Intent(context,MyService.class);
        //starting it manually first because API 19+ doesn't deliver exact alarms, but we want a run now
        context.startService(intent);
        intent = new Intent(context,ServiceReceiver.class);
        int minutes=PreferenceManager.getDefaultSharedPreferences(context).getInt("timer",R.integer.timer_def);
        final PendingIntent pendingIntent= PendingIntent.getBroadcast(context.getApplicationContext(),1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),minutes*60000,pendingIntent);
    }

接收方(WakefulBroadcastReceiver)的onreceive方法:

@Override
public void onReceive(Context context, Intent intent) {
    Log.e("SERV-RECEIVER ","triggered");
    startWakefulService(context,new Intent(context,MyService.class));
}

请注意,发生这种情况时,我看不到 SERV-RECEIVER 日志,因此即使接收器也不会被触发。我的意图服务打开一个 SQLite 数据库,做一些工作,关闭它,然后释放唤醒锁。接收者在 android manifest 中注册,没有意图:

<receiver android:name=".ServiceReceiver" android:enabled="true"/>

我尝试使用 adb 命令将设备设置为打盹模式,并将应用程序设置为手动待机,在我唤醒设备后它仍然可以正常工作。仅当我将其放置一段时间时才会出现此问题。使用 adb shell dumpsys 警报,我注意到这始终是可见的(即使在警报停止后

trigerring):
u0a229:com.isoc.android.monitor +2s384ms running, 37 wakeups:
    +2s384ms 37 wakes 37 alarms, last -6m33s804ms:
      *walarm*:com.isoc.android.monitor/.ServiceReceiver

但是当问题发生时这会消失(来自批处理部分):

ELAPSED_WAKEUP #0: Alarm{99ac864 tag *walarm*:com.isoc.android.monitor/.ServiceReceiver type 2 when 340221779 com.isoc.android.monitor}
      tag=*walarm*:com.isoc.android.monitor/.ServiceReceiver
      type=2 whenElapsed=-971ms when=-5s971ms
      window=+3m45s0ms repeatInterval=300000 count=0 flags=0x0
      operation=PendingIntent{14dfacd: PendingIntentRecord{82c6f82 com.isoc.android.monitor broadcastIntent}}

我注意到,在我的 android API 15 手机上,这个问题并没有发生。仅在我的 Android 棉花糖手机上...任何帮助将不胜感激 :) 谢谢

【问题讨论】:

    标签: android android-alarms android-background android-doze-and-standby


    【解决方案1】:

    这是我的 WakeLocker 课程

      public class WakeLocker {
    private static PowerManager.WakeLock wakeLock;
    
    public static void setWakeLock(Context ctx) {
        PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
    
        if (wakeLock != null) wakeLock.release();
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Vocabulary_Alarm");
    
        wakeLock.acquire();
    }
    
    public static void resetWakeLock() {
        if (wakeLock != null) wakeLock.release();
        wakeLock = null;
    } }
    

    在接收器上接收

      public class AlarmReceiver extends WakefulBroadcastReceiver {
    
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        WakeLocker.setWakeLock(context);
    
    
        Intent intentone = new Intent(context.getApplicationContext(), YourActivity.class);
        intentone.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        intentone.putExtra("startTest",true);
        context.startActivity(intentone);
    

    但是在这里,如果您看到我启动了所需的活动而不是服务。如果您的问题在使用 WakeLock 后仍未解决,您也尝试启动相关的 Activiy。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多