简体   繁体   English

如何在Android Oreo或更高版本上设置闹钟?

[英]How to set Alarm on android Oreo or Above?

I have implemented AlaramManager with background service , It works fine on devices below Oreo. 我已经使用background service实现了AlaramManager ,它在Oreo以下的设备上运行良好。 But doesn't work on android Oreo and above as Google stopped background services from working on Oreo and Above. 但不适用于android Oreo及更高版本,因为Google停止了在android Oreo及更高版本上运行background services

Is there any source code or any kind resource which will help me to set alarms on android Oreo or above devices? 是否有任何源代码或任何种类的资源可帮助我在android Oreo或以上设备上设置alarms

After searching for a while I found out about JobsIntentService , but couldn't found enough information about it. 搜索了一段时间后,我发现了JobsIntentService ,但是找不到足够的信息。 And I don't know if it is the way to go. 而且我不知道这是不是要走的路。 Basically, I want to show notifications on particular dates and times. 基本上,我想显示特定日期和时间的notifications

Thanks in Advance. 提前致谢。

Please try bellow method it will work for you. 请尝试以下方法,它会为您服务。

/**
 * Method for start Alarm on defined minute
 *  @param minutes Minute when you want to start after current time
 * @param context
 */
public static void startAlarm(Context context, int minutes) {
    Logger.print("AlarmReceiver startAlarm  called");
    Intent alarmIntent = new Intent(context, WakefulBroadcastReceiverService.class);
    alarmIntent.setAction("testAPP");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123451, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    manager.cancel(pendingIntent);
    long alarmPeriodicTime = System.currentTimeMillis() + Utils.getTimeInMilliSec(Constant.TimeType.MINUTE, minutes);
    if (Build.VERSION.SDK_INT >= 23) {
        manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
    } else if (Build.VERSION.SDK_INT >= 19) {
        manager.setExact(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
    } else {
        manager.set(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
    }
}

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

相关问题 如何在android Oreo及以上版本中跟踪用户 - How to track user in android Oreo and above version 如何在 Android Oreo 及更高版本的通知托盘中显示带有图片的 Android 通知? - How to show Android notification with picture in notification tray in Android Oreo and above? 如何以编程方式在Android Nougat,Oreo及更高版本中阻止通话? - How to Block Call in Android Nougat, Oreo and above programatically? 如何在 Android Oreo (8.1) 或更高版本中从 URI 获取文件路径 - How to Get File Path from URI in Android Oreo (8.1) or above 如何在Android Oreo及以上版本中找到wifi hotpsot的SSID和密码? - How to find SSID and password of the wifi hotpsot in android Oreo and above? 如何避免Android Oreo及更高版本上的抬头通知? - How to avoid Heads-up notification on android Oreo and above? 如何从 Android Oreo 及更高版本的下载文件夹上传文件? - How to upload file from downloads folder in Android Oreo and above? 如何在Android中使用通知设置警报 - How to set alarm with notifications in Android 如何在Android Java中设置警报 - How to set an alarm in android java 如何设置Android提醒/警报? - How to set android reminder/alarm?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM