定时警报永远不会调用接收器类别 [英] Scheduled alarm never calls receiver class

查看:61
本文介绍了定时警报永远不会调用接收器类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个每5分钟触发一次的闹钟.

I'm trying to set an alarm to fire every 5 minutes.

这是设置警报的代码:

   @Override
public void scheduleAlarmManager() {
    Timber.i("After SignIn sets AlarmManager");
    // broadcast
    Intent intent = new Intent(this, PatientAlarmReceiver.class);
    intent.setAction(PATIENT_START_ALARM_ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            this, REQUEST_CODE, intent, 0);

    // and set alarmManager
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

    Calendar currentCal = Calendar.getInstance();
    long currentTIme = currentCal.getTimeInMillis();

    // if there's not an Alarm already set then set one
    if (!isAlarmSet(this)) {
        Timber.i("Alarm not set - so set one");
        alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                currentTIme + TWO_MINS_DURATION, TWO_MINS_DURATION, pendingIntent);
    }

}

并且我可以验证我是否正确设置了警报,因为我在logcat中看到了用Timber记录的消息.

and I can verify that I set the alarm correctly since I see in my logcat the messages I log with Timber.

我的Receiver类是:

My Receiver class is :

public class PatientAlarmReceiver extends BroadcastReceiver {

public static final String TAG = "PATIENT-ALARM-RECEIVER";
public static final String PATIENT_START_ALARM_ACTION = "bp.headsup.receivers.alarm.patient";

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "Inside OnReceive Patient");
    Timber.i("Inside OnReceive Patient");

    if (intent == null || intent.getAction() == null) {
        return;
    }

    String action = intent.getAction();
    if (PATIENT_START_ALARM_ACTION.equalsIgnoreCase(action)) {
        onStartCheckForConnectionRequest(context);
    }
}

/**
 * If is connected to network starts services
 */
private void onStartCheckForConnectionRequest(Context context) {
    NetworkUtils networkUtils = new NetworkUtils(context);
    if (networkUtils.isNetworkConnected()) {
        Intent checkForConnRequestIntent = new Intent(context, PatientCheckForConnectionRequestService.class);
        context.startService(checkForConnRequestIntent);
        Timber.i("Starts Service From PatientALARMMANAGER");
    }
}

}

我已经在清单中声明了:

And I have declared in Manifest :

        <!-- Receivers -->
    <receiver
        android:name="bp.headsup.receivers.PatientAlarmReceiver" />

如果我也跑步:adb shell dumpsys alarm 我可以看到:

Also if I run : adb shell dumpsys alarm I can see :

  ELAPSED_WAKEUP #0: Alarm{42d804e8 type 2 bp.headsup.mock}
operation=PendingIntent{42d0c230: PendingIntentRecord{42d0f000 bp.headsup.mock broadcastIntent}}

上面的响应中的模拟是我正在使用的sourceSet-我不知道它是否与此有关.

Mock in the above response is the sourceSet I'm using - dont know if it has anything to do with this I just mention it.

问题是我从来没有在logcat中读取Receiver类在onReceive中的消息,并且显然没有任何服务启动.任何人都可以帮忙吗?我使用的是与kitKat 4.4(api 19)一起运行的设备,但我也已经在模拟器上进行了尝试,结果是相同的.

The problem is I never read in logcat the messages I have in onReceive on my Receiver class, and obviously no service starts. Anyone can help with that ? I'm using a device which runs with kitKat 4.4 (api 19) but I have tried it with an emulator too and the result was the same.

推荐答案

您正在设置ELAPSED_REALTIME警报,该警报基于自上次引导以来的时间.但是,您传递的是基于挂钟"的开始时间,因此您的闹钟实际上在将来设置得很远.

You're setting an ELAPSED_REALTIME alarm, which is based on the time since the last boot. However, you're passing it a starting time based on the "wall clock", so your alarm is actually set quite far in the future.

您可以将警报更改为RTC类型,也可以从SystemClock.elapsedRealtime()获取开始时间.根据您描述的行为,保持经过的类型并更正开始时间似乎是适当的.

You can either change the alarm to an RTC type, or get the starting time from SystemClock.elapsedRealtime(). Given your described behavior, keeping the elapsed type and correcting the starting time seems appropriate.

这篇关于定时警报永远不会调用接收器类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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