【发布时间】:2019-05-09 09:53:53
【问题描述】:
我在每天的特定时间创建了一个预定的重复警报,使用 AlarmManager 在扩展 BroadcastReceiver 的类中触发通知。但是从设置了 AlarmManager 的活动中永远不会调用 onReceive 方法。
我正在使用 Android Oreo 来测试应用程序,因此我创建了一个方法 createNotificationChannel() 来设置 NotificationChannel 并从我的 MainActivity onCreate() 方法中调用它。
public void createNotificationChannel() {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.O) {
// Create the NotificationChannel with all the parameters.
NotificationChannel notificationChannel = new NotificationChannel
(PRIMARY_CHANNEL_ID,
"My Notification",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription
("My Notification Description");
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
带有一些变量:
private NotificationManager mNotificationManager;
private static final int NOTIFICATION_ID = 0;
private static final String PRIMARY_CHANNEL_ID =
"primary_notification_channel";
然后我设置了一个按钮,onClick() 方法调用 startAlarm() 方法如下:
public void startAlarm(int aHour, int aMinutes) {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent notifyPendingIntent = PendingIntent.getBroadcast
(this, NOTIFICATION_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,aHour);
calendar.set(Calendar.MINUTE, aMinutes);
alarmManager.setInexactRepeating
(AlarmManager.ELAPSED_REALTIME_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, notifyPendingIntent);
}
最后在 AlarmReceiver.java 类中,在 onReceive() 中如下:
public void onReceive(Context context, Intent intent) {
mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
callNotification(context);
}
private void callNotification(Context context) {
Intent contentIntent = new Intent(context, DRSetting.class);
PendingIntent contentPendingIntent = PendingIntent.getActivity
(context, NOTIFICATION_ID, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, PRIMARY_CHANNEL_ID);
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon_mynotif)
.setContentTitle("My Title")
.setContentIntent(contentPendingIntent)
.setContentText("Reminder for you!")
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentInfo("Info");
mNotificationManager.notify(NOTIFICATION_ID, builder.build());
}
在 AndroidManifest.xml 中,我放了这个:
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
权限如下:
<uses-permission android:name="android.permission.SET_ALARM" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
所以,有人可以帮助我吗,因为我找不到为什么永远不会调用 onReceive() 方法的问题,因此在 callNotification() 中没有触发任何通知。
【问题讨论】:
标签: android notifications alarmmanager