我开发了一个应用程序,它使用AlarmManager来设置需要在一年中的某个特定时间触发的群警报(通常大约50个).这是我正在使用的代码,因为4.4 kitkat更改了AlarmManager.
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
long setDate = fireDate.getTime(); // it's a calendar date defined above
Intent intent = new Intent(LOCAL_DISPLAY_MESSAGE_ACTION);
PendingIntent pending = PendingIntent.getBroadcast(ctx,
id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.RELEASE.startsWith("6")) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, setDate, pending);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
am.setExact(AlarmManager.RTC_WAKEUP, setDate, pending);
} else {
am.set(AlarmManager.RTC_WAKEUP, setDate, pending);
}
Run Code Online (Sandbox Code Playgroud)
除了上面的代码,我正在使用清单中正确定义的广播接收器.
public class LocalReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
PushWakeLocker.acquire(context);
// do some stuff
PushWakeLocker.release();
}
}
Run Code Online (Sandbox Code Playgroud)
更多信息可能会有所帮助.
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
Run Code Online (Sandbox Code Playgroud)
几个月以前,我一直只是从三星设备(5.0 …
android alarmmanager samsung-mobile android-5.0-lollipop android-5.1.1-lollipop