Alarm in Android stops when some hours has passed

♀尐吖头ヾ 提交于 2019-12-10 17:57:00

问题


I'm making an app that needs to do some tasks repeteadly, usually between 1 and 5 minutes.

I have tried .setRepeating, setInexactRepeating and .set (the last one re-setting the alarm when the task is done)

When the alarm fires a service is started.

Usually it works well, but sometimes the alarm suddenly stops firing, no matter what method I use.

I have tested in Android 5.1 and 4.4.2.

Why is this happening? Is this a known bug or something?

Here is the method I use to set the alarm, the commented code is the other method I tried:

public void setAlarm(Context context) {
    alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    /*alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + 1000,
            PreferenceHelper.readLong(PreferenceHelper.PREF_TIME_TO_REFRESH), alarmIntent);*/
    alarmMgr.set(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + PreferenceHelper.readLong(PreferenceHelper.PREF_TIME_TO_REFRESH), alarmIntent);

    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

As I said before, if I use .set I call the method again when the service finishes the asigned task. (This works too, but again the alarm suddenly stops firing after a few hours).

public class AlarmReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, WifiCheckerService.class);
    startWakefulService(context, service);
}

回答1:


I actually had the same problem and looking to transfer to SyncAdapter's instead, but having other issues with that. So not suggesting this will fix your problem.

Problem isn't the Alarm Manager at all. My application targets API > 23 and testing with API 24 phone and still getting this issue. Problem is that some manufacturers are creating their own Application Manager on top of Android and that is killing your application for resource/battery reasons which stops your application and any Alarms associated with it from triggering.

Check out this post to check if the application is in stopping mode. Android AlarmManager stops when activity die

I noticed on some phones this does not happen and on some phones it does...

If this is wrong and you do find out what is actually wrong, please let me know as Sync Adapters are killing me....




回答2:


As a general principle, although you're running your app in devices with API < 23, as you target API 23 you should be using setExactAndAllowWhileIdle instead of set.

What you describe: alarm suddenly stops firing after a few hours sounds like what would happen in an M device. I wonder whether that behavior is also forced intentionally in pre-M devices when targeting API 23...I'd need to look at Android source code get an answer to that.



来源:https://stackoverflow.com/questions/34567873/alarm-in-android-stops-when-some-hours-has-passed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!