简体   繁体   English

Android AlarmManager在特定时间未调用PendingIntent

[英]Android AlarmManager is not calling PendingIntent at specific time

I want to run service at specific time and for this I am using AlarmManager.My code below. 我想在特定时间运行服务,为此,我正在使用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();
    }
}

The problem is that, AlarmManager is not calling my Service,which I gave in PendingIntent. 问题是,AlarmManager没有调用我在PendingIntent中提供的服务。 But, when I am trying to call AlarmManager like it is below, it's working! 但是,当我尝试像下面这样调用AlarmManager时,它正在工作! Why? 为什么?

 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();
    }
}

I tried almost all AlarmManager methods(like setExact(), set(), setRepeating(), setAlarmClock()) still working only when I'm passing System.currentTimeMillis() 我尝试了几乎所有的AlarmManager方法(例如setExact(),set(),setRepeating(),setAlarmClock())仅在我通过System.currentTimeMillis()时仍然有效

Try this code add PendingIntent in AlarmManager 尝试将此代码在AlarmManager中添加PendingIntent

public 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);
}

Also add PendingIntentReceiver in AndroidManifest too 还要在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));
    }
}

I later versions of Android you should actually use setAlarmClock : 在更高版本的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);
    }
}

This is how you could check the versions: 这是检查版本的方法:

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;
    }
}

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

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