AlarmManager不调用BroadcastReceiver

时间:2019-05-09 09:53:53

标签: android notifications alarmmanager

我每天每天的特定时间使用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()中没有触发过的通知。

1 个答案:

答案 0 :(得分:0)

由于JournalDev,我发现导致警报无法触发的问题。这是因为自Android Oreo以来,在AndroidManifest.xml中注册时,隐式广播接收器将无法工作。 相反,我们应该使用显式广播接收器。因此,我修改了代码,添加了两个附加方法,如下所示:

@Override
protected void onResume() {
    super.onResume();

    IntentFilter filter = new IntentFilter();
    filter.addAction("android.app.action.NEXT_ALARM_CLOCK_CHANGED");
    registerReceiver(alarmReceiver, filter);

    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(NOTIFICATION_ID);
}

@Override
protected void onStop() {
    super.onStop();
    unregisterReceiver(alarmReceiver);
}

此外,如果要设置特定日期以触发警报,则应注意日历月值的设置。 Java中的月份从0到11开始,因此从DatePicker转换月份值时我们需要为-1。我希望这个答案可以帮助其他面临相同问题的人。