简体   繁体   English

Android-长时间未触发警报有时

[英]Android - Alarm doesn`t fire sometimes after long period

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. 我实现了AlarmManager,使我的应用程序用户可以选择在当天或第二天的任何时间启动某些服务。 So eg the user might choose to set the time for my service to tomorrow at 08:00 am and then starts the service. 因此,例如,用户可以选择将我的服务时间设置为明天的上午08:00,然后启动服务。

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. 看来,如果必须等待超过4个小时,则表明该设备不再工作,并且不会调用我的服务。 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: 您可以查看以下alarmmanager的代码:

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. 正如我告诉您的,如果它不起作用,它将在启动服务之前停止。

EDIT: I have an idea. 编辑:我有一个主意。 I read something in this thread: Android long running service with alarm manager and inner broadcast receiver 我在以下线程中读到了一些东西: 具有警报管理器和内部广播接收器的Android长期运行服务

It might be, that my constructor for the intent "someIntent" only works for BroadcastReceivers declared in the manifest file. 可能是,我的意图“ someIntent”的构造函数仅适用于清单文件中声明的BroadcastReceivers。 So in my case I should maybe use someIntent = new Intent("START_SERVICE") instead of someIntent = new Intent(); someIntent.setAction("START_SERVICE") 因此,就我而言,我应该使用someIntent = new Intent("START_SERVICE")代替someIntent = new Intent(); someIntent.setAction("START_SERVICE") someIntent = new Intent(); someIntent.setAction("START_SERVICE") . 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

If the device is shutdown and start up then you will not get the alarm maanger broadcast receiver. 如果设备关闭并启动,则不会收到警报maanger广播接收器。 Implement OnBootReceiver which will receive the OnBoot completed and there you can start your pending alarms that were not fired. 实现OnBootReceiver,它将接收完成的OnBoot,然后您可以在其中启动未触发的未决警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM