广播接收器从AlarmManager没有得到所谓的 [英] BroadcastReceiver from AlarmManager not getting called

查看:174
本文介绍了广播接收器从AlarmManager没有得到所谓的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有扩展 IntentService A类。当我的应用程序重新启动(也是应用程序启动时),在这个类的东西被调用。它的加载的某些设置,并做一些事情的背景(这是一种生产力工具/实用程序)。

I have a class that extends IntentService. The stuff in this class is called when my app reboots (and also on app startup). It loads some settings and does some stuff in the background (it's a productivity tool/utility).

IntentService ,装载我的时候,让我们称之为的东西的,决定是否通过<$ C $添加一些警报C> AlarmManager 。我有一些接受不同的参数,并根据这些参数的一些方法,一个BroadcastReceiver。这里是code在我调度报警:

This IntentService, when loading my, let's call it "stuff", decides whether or not to add some alarms via AlarmManager. I have a BroadcastReceiver that receives some different parameters and does some method based on those parameters. Here is the code in which I am scheduling the alarms:

public void addAlarm(int id, int hourFrom, int hourTo, int minuteFrom, int minuteTo)
{
    AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
    // Enable
    Intent intent = new Intent(Controller.KEY_CONTROLLER_ACTION);
    intent.putExtra(Controller.KEY_CONTROLLER_ACTION, Controller.KEY_ACTION_ENABLE);
    intent.putExtra(Controller.KEY_CONTROLLER_ID, id);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            this,
            id,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hourFrom);
    calendar.set(Calendar.MINUTE, minuteFrom);
    calendar.set(Calendar.SECOND, 00);
    int hours_24 = 24 * 60 * 60 * 1000;
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), hours_24, pendingIntent);

    // Cancel
    Intent cancelIntent = new Intent(Controller.KEY_CONTROLLER_ACTION);
    intent.putExtra(Controller.KEY_Controller_ACTION, Controller.KEY_ACTION_END);
    intent.putExtra(Controller.KEY_Controller_ID, id);
    PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(
            this,
            id,
            cancelIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    calendar.set(Calendar.HOUR_OF_DAY, hourTo);
    calendar.set(Calendar.MINUTE, minuteTo);
    calendar.set(Calendar.SECOND, 00);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), hours_24, cancelPendingIntent);

这里的问题:广播接收器(Controller类)只的有时的有的onReceive 调用。如果是这样,这是推迟了几分钟(没什么大不了的,但它是一个,很多时候它永远不会被调用的问题)。在第二的报警器( cancelIntent 的)的从不被调用。

Here's the problem: The BroadcastReceiver (Controller class) only sometimes has its onReceive called. When it does, it's a few minutes delayed (no big deal; but it is a problem that many times it never gets called). The second alarm (cancelIntent) never gets called.

我的控制器广播接收器是正确申报清单。我怀疑它是与这两个意图 s是相同的(两者对类型的操作,以及一个额外的ID)。

My Controller BroadcastReceiver is properly declared the manifest. I suspect it has something to do with both Intents being identical (both have extras for type of action, as well as an ID).

我是什么做错了吗?另外,什么是更多的正确的方式去拥有从-根据报警?这样,我的意思是我需要一些方法在一定的时间发生,和不同的方法。在一个底的时间(在同一类控制器广播接收器

What am I doing wrong? Alternatively, what is the more proper way to go about have a from-to based alarm? By this, I mean I need some method to happen at a certain time, and a different method (in the same class; Controller BroadcastReceiver) at an "end" time.

推荐答案

我修改舱单采取多种行动(真正的行动,而不是说我是通过<$ C $做伪行动C> addExtra )。

I modified my manifest to take multiple action (real actions, rather than pseudo-actions that I was doing via addExtra).

<receiver android:name="com.mycompany.myapp.Controller">
        <intent-filter>
            <action android:name="com.mycompany.myapp.Controller.START"/>
        </intent-filter>
        <intent-filter>
            <action android:name="com.mycompany.myapp.Controller.END" />
        </intent-filter>
    </receiver>

然后,当我添加了报警与意图,我一定要叫的setAction ,指定字符串行动。在的onReceive 广播接收器我称之为的getAction() (返回一个字符串)。瞧!两个报警器断火如预期,我就能采取行动的类型,并进行相关的我的方法。

Then, when I add the alarms with the Intent, I make sure to call setAction, specifying the String action. In the onReceive of the BroadcastReceiver I call getAction() (returns a String). And voila! Both alarms fire off as intended, and I can get the type of action and perform my associated methods.

注意:与@Chitrang的帮助下(通过评论的问题)回答。指出,意图已得到取消,因为已经有预定的报警 IntentSender

Note: Answered with the help of @Chitrang (via comments to the question). Pointed out that the Intent was getting canceled because there was already an alarm scheduled for the IntentSender.

这篇关于广播接收器从AlarmManager没有得到所谓的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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