Android 定时报警从不调用接收器类
我正试着每5分钟设置一次警报 这是设置报警的代码:Android 定时报警从不调用接收器类,android,android-alarms,Android,Android Alarms,我正试着每5分钟设置一次警报 这是设置报警的代码: @Override public void scheduleAlarmManager() { Timber.i("After SignIn sets AlarmManager"); // broadcast Intent intent = new Intent(this, PatientAlarmReceiver.class); intent.setAction(PATIENT_START_ALARM_AC
@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);
}
}
我可以验证我是否正确设置了警报,因为我在日志猫中看到了我使用Timber
记录的消息
我的接受者等级是:
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");
}
}
}
我在舱单上声明:
<!-- Receivers -->
<receiver
android:name="bp.headsup.receivers.PatientAlarmReceiver" />
上面回复中的Mock是我正在使用的源代码集-不知道它是否与此有关,我只是提到了它
问题是我从未在logcat中读取我在Receiver类上的
onReceive
中的消息,显然没有服务启动。有人能帮忙吗?我正在使用一个运行kitKat 4.4(api 19)的设备,但我也用模拟器进行了尝试,结果是一样的。您正在设置一个基于上次启动后的时间的已用\u REALTIME
报警。然而,您正在通过一个基于“挂钟”的开始时间,因此您的闹钟实际上设置在相当远的将来
您可以将闹钟更改为
RTC
类型,或从SystemClock.elapsedRealtime()
获取开始时间。根据您所描述的行为,保持已用时间类型并更正开始时间似乎是合适的。我认为您尚未在menifest中设置接收器的操作(启动完成)。您正在设置已用时间警报,但您正在为其设置“挂钟”开始时间。将闹钟更改为RTC
类型,或者从SystemClock.elapsedRealtime()
@MikeM获取开始时间。对的成功了!谢谢
ELAPSED_WAKEUP #0: Alarm{42d804e8 type 2 bp.headsup.mock}
operation=PendingIntent{42d0c230: PendingIntentRecord{42d0f000 bp.headsup.mock broadcastIntent}}