数小时过去后,Android中的警报停止

编程入门 行业动态 更新时间:2024-10-19 23:33:15
本文介绍了数小时过去后,Android中的警报停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在制作一个需要重复执行某些任务的应用程序,通常需要1到5分钟。

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

我尝试了 .setRepeating , setInexactRepeating 和 .set (最后一个在任务执行时重新设置警报)

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.

我已经在Android 5.1和4.4.2。中进行了测试。

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

正如我之前所说,如果使用 .set 服务完成分配的任务后,我再次调用该方法。 (这也可行,但是警报会在几个小时后突然停止发出警报。)。

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

推荐答案

我实际上遇到了同样的问题并希望转移到SyncAdapter,但与此同时还有其他问题。因此,不建议这样做可以解决您的问题。

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.

问题根本不是警报管理器。我的应用程序的目标是API> 23,并使用API​​ 24电话进行测试,但仍然遇到此问题。问题是某些制造商在Android之上创建了自己的应用程序管理器,并且由于资源/电池原因而杀死了您的应用程序,这会停止您的应用程序以及与之相关的所有警报的触发。

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.

查看此帖子以检查应用程序是否处于停止模式。 Android AlarmManager在活动终止时停止

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....

更多推荐

数小时过去后,Android中的警报停止

本文发布于:2023-11-27 07:28:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   小时   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!