[英]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.