以下是设置广播接收器的代码。
PendingIntent pendingIntentScheduler = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntentScheduler);
和BROADCASTRECIEVER类的代码
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
Log.d("onReceive", "this is broadcast reciever");
}
Android Manifest Permisssions。
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application>
<receiver android:name=".name.recieverName" >
<intent-filter>
<action android:name="com.intentIWantToOpenOnRecievingBroadcast" />
</intent-filter>
</receiver>
</application>
此代码与api的工作正常至19岁。它不适用于Lollipop api 21.请问有人发现任何问题吗?。
答案 0 :(得分:1)
看一看 https://code.google.com/p/android/issues/detail?id=82001 要么 AlarmManager not fires on time in Android Lollipop
答案 1 :(得分:0)
试试这个
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, AlarmActive, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= 19)
am.setExact(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), pi);
else
am.set(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), pi);