【问题标题】:Android daily notifications do not work properlyAndroid 每日通知无法正常工作
【发布时间】:2015-08-21 15:43:12
【问题描述】:

我想在每天早上 9 点通过我的应用显示通知。

所以我正在使用通知管理器、警报管理器、广播接收器和服务来实现这一点。

但是我有一个问题,因为通知是随机显示的。当我第一次启动应用程序并设置时间时,它工作正常,但后来应用程序触发并随机显示通知。

我该如何解决?

这是我的代码:

MainActivity

@Override
protected void onStart() {
    super.onStart();
    setAlarm();
}

public void setAlarm(){


    Calendar calendar = Calendar.getInstance();
    Calendar now = Calendar.getInstance();


    calendar.set(Calendar.HOUR_OF_DAY, 15);
    calendar.set(Calendar.MINUTE, 43);
    calendar.set(Calendar.SECOND, 0);

    if(calendar.getTime().after(now.getTime())) {
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        alarmIntent = new Intent(MainActivity.this, HoroscopeNotification.class);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);        }

 }

HoroscopNotification(广播接收器)

public class HoroscopeNotification extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent arg1) {
        showNotification(context);
    }

    private void showNotification(Context context) {
        Intent service1 = new Intent(context, AlarmService.class);
        context.startService(service1);
    }
}

报警服务

public class AlarmService extends Service {
    private static final int NOTIFICATION_ID = 1;
    private NotificationManager notificationManager;
    private PendingIntent pendingIntent;

    @Override
    public IBinder onBind(Intent arg0)
    {
         return null;
    }

    @SuppressWarnings("static-access")
    @Override
    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);
        Context context = this.getApplicationContext();
        notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
        Intent mIntent = new Intent(this, MainActivity.class);
        pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("Horoskop");
        builder.setContentText("Pročitajte današnji horoskop");
        builder.setSmallIcon(R.drawable.ic_bik);
        builder.setAutoCancel(true);
        builder.setContentIntent(pendingIntent);

        notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }
}

【问题讨论】:

  • 您应该添加日志以查看实际注册的日历是什么。这将使您清楚地了解实际注册的内容以及应该触发的确切时间。

标签: android android-service android-notifications android-broadcast android-alarms


【解决方案1】:

您会在 Android SDK 参考资料中注意到AlarmManager.setRepeating() 状态:

注意:从 API 19 开始,所有重复警报都是不准确的。如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,并如上所述重新安排每次。 targetSdkVersion 早于 API 19 的旧版应用程序将继续将其所有警报(包括重复警报)视为准确。

您需要在 pre-APIv19 上使用 AlarmManager.set(),在 APIv19+ 上使用 AlarmManager.setExact()。当您的PendingIntent 被触发并且您在BroadcastReceiver.onReceive() 中收到您的广播时,您可以为第二天设置另一个确切的闹钟。

【讨论】:

  • 我应该先设置活动警报还是其他地方?
  • 你好,我最初需要在哪里设置闹钟?
  • 是的。当用户启动您的应用程序时。所以是的,你的 MainActivity。
  • 就在他第一次启动应用程序的时候?然后在 BroadcastReciever 上应该设置第二天的通知。
  • 是的。然后将方法设置为静态并将它们移动到一个名为 DailyAlarm 或其他东西的新类中可能会很好。为了代码的组织和清晰。
【解决方案2】:

Alarm Manager Example

我认为你应该按照上面的链接。从我的角度来看,您的设计模式(在Activity 类中设置警报不是一个好方法)。相反(如上面的答案所示),您应该从服务中设置警报。通知代码也位于BroadcastReceiver 类,方法OnReceive 中(在示例中,注释为“将您的代码放在这里”)。

祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 2019-11-03
    • 2016-11-27
    相关资源
    最近更新 更多