我想在特定时间运行服务,为此我使用的是AlarmManager.My代码。
public static void setServiceAlarm(Context context, boolean isOn) {
Intent i = new Intent(context, RefreshingMedicationDataService.class);
PendingIntent pi = PendingIntent.getService(
context, 0, i, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 51);
AlarmManager alarmManager = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
if (isOn) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pi);
} else {
alarmManager.cancel(pi);
pi.cancel();
}
}
问题是,AlarmManager没有调用我在PendingIntent中提供的服务。 但是,当我试图将AlarmManager调用为低于它时,它正在工作!为什么?
public static void setServiceAlarm(Context context, boolean isOn) {
Intent i = new Intent(context, RefreshingMedicationDataService.class);
PendingIntent pi = PendingIntent.getService(
context, 0, i, 0);
AlarmManager alarmManager = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
if (isOn) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), pi);
} else {
alarmManager.cancel(pi);
pi.cancel();
}
}
我尝试了几乎所有的AlarmManager方法(比如setExact(),set(),setRepeating(),setAlarmClock())只有在我通过System.currentTimeMillis()
时才能正常工作
答案 0 :(得分:0)
尝试此代码在 AlarmManager
中添加 PendingIntentpublic void addPendingIntentAlarmManager() {
final PendingIntent pendingIntent = createPendingIntent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, notification.getTime(), pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, notification.getTime(), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, notification.getTime(), pendingIntent);
}
}
private PendingIntent pendingIntent() {
final Intent intent = new Intent(app, NotificationReceiver.class);
intent.setAction("simplePI 1");
return PendingIntent.getBroadcast(context, 10, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
另外在AndroidManifest中添加 PendingIntentReceiver
public class PendingIntentReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final ComponentName comp = new ComponentName(context.getPackageName(), **YOURSERVICE**.class.getName());
startWakefulService(context, intent.setComponent(comp));
}
}
答案 1 :(得分:0)
我在Android的更高版本中应该实际使用setAlarmClock
:
private void scheduleAlarm(AlarmManager alarmManager, long targetTime, PendingIntent pendingIntent, Intent intent) {
if(VersionCheckUtils.isLollipopOrLater()) {
logAlarm(targetTime, pendingIntent, intent, "Lollipop");
alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(targetTime, pendingIntent),
pendingIntent);
}
else {
logAlarm(targetTime, pendingIntent, intent, "Kitkat");
alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetTime, pendingIntent);
}
}
这是您检查版本的方法:
public class VersionCheckUtils {
private static final int LOLLIPOP = 21;
static final int KITKAT = 20;
private static final int OREO = 26;
public static boolean isKitKatOrLater() {
return Build.VERSION.SDK_INT >= KITKAT;
}
public static boolean isLollipopOrLater() {
return Build.VERSION.SDK_INT >= LOLLIPOP;
}
public static boolean isOreoOrLater() {
return Build.VERSION.SDK_INT >= OREO;
}
}