Android的 - 报警火`吨经过长时间有时 [英] Android - Alarm doesn`t fire sometimes after long period

查看:142
本文介绍了Android的 - 报警火`吨经过长时间有时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有我的应用程序了好几天的一个大问题。我appologize如果我的英语不是提前让本地人。我实现了一个AlarmManager给我的应用程序的用户在当前或下一天中的任何时间启动某个服务的选项。因此,如用户可以选择在08:00时至设置为我服务,明天的时间,然后启动该服务。

I have a big problem with my app for several days now. I appologize if my english is not so native in advance. I implemented an AlarmManager to give the user of my app the option to start a certain service at any time of the current or the next day. So e.g. the user might choose to set the time for my service to tomorrow at 08:00 a.m. and then starts the service.

报警经理现在应该从现在开始等待计算的时间到所选择的时间(我手动计算的时间也和它是正确的!),然后启动该服务。我现在的问题是,有时alarmmanager开始我的服务和somtimes没有。看来,如果它要等待让说,4个多小时它不工作的更多,我的服务不叫。我设置了所有neccessary许可,否则就不能在所有的工作。你可以看看下面的alarmmanager的code:

The alarm manager should now wait the calculated time from now till the chosen time (i calculated the time also manually and it is correct!) and then start the service. My problem now is that sometimes the alarmmanager is starting my service and somtimes not. It seems that if it has to wait for lets say more than 4 hours it is not working any more and my service is not called. I have set all neccessary permission otherwise it would not work at all. You can have a look at the code of the alarmmanager below:

someIntent = new Intent();
someIntent.setAction("START_SERVICE");
AlarmManager alarams ;

alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, someIntent, PendingIntent.FLAG_CANCEL_CURRENT);
alarams = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarams.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+delay, alarmIntent);

广播接收机是这样实现(已注册!):

The broadcast receiver is implemented like this (and it is registered!):

alarmReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                Intent myIntent = new Intent(MainActivity.this, MyService.class);
                startService(myIntent);
            }
        };

正如我告诉你,如果它不工作就开始服务之前停止。

As I told you, if it is not working it stops before starting the service.

编辑:
我有个主意。我读了这个线程的内容:<一href=\"http://stackoverflow.com/questions/9138597/android-long-running-service-with-alarm-manager-and-inner-broadcast-receiver\">Android报警经理和内部广播接收器长时间运行的服务。

I have an idea. I read something in this thread: Android long running service with alarm manager and inner broadcast receiver

这也许,是我的意图someIntent构造只适用于在manifest文件中声明BroadcastReceivers。所以在我的情况下,也许我应该用 someIntent =新意图(START_SERVICE)而不是 someIntent =新意图(); someIntent.setAction(START_SERVICE)。这种效应被称为隧道 - 我会弄清楚,如果它的工作原理,我将在这里发布我的经验。谢谢你的提及线程上很好解释的答案!如果您有任何其他的想法,或像我一样的经历,请让我知道!
动车组

It might be, that my constructor for the intent "someIntent" only works for BroadcastReceivers declared in the manifest file. So in my case I should maybe use someIntent = new Intent("START_SERVICE") instead of someIntent = new Intent(); someIntent.setAction("START_SERVICE"). This effect is called tunneling - i will figure it out, if it works i will post my experience here. Thanks for the well explained answer on the mentioned thread! If you have any other ideas or the same experiences like me please let me know! eMu

推荐答案

如果该设备是关机和启动,那么你不会得到报警maanger广播接收机。
实施OnBootReceiver将收到ONBOOT完成,那里你可以启动不解雇你的未决报警。

If the device is shutdown and start up then you will not get the alarm maanger broadcast receiver. Implement OnBootReceiver which will receive the OnBoot completed and there you can start your pending alarms that were not fired.

这篇关于Android的 - 报警火`吨经过长时间有时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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