在 android 中设置定期通知

Set a periodic notification in android

我正在尝试在 android 中创建定期通知,我几乎已经实现了,问题是使用我的代码它打开了应用程序而不是创建通知,这是我的代码:

NotificationCompat.Builder mBuilder =
           new NotificationCompat.Builder(this)
                   .setSmallIcon(R.mipmap.ic_launcher)
                   .setContentTitle(this.getText(R.string.notificationMessage))
                   .setContentText("")
                   .setAutoCancel(true);

    Intent resultIntent = new Intent(this, MainActivity.class);

    // Because clicking the notification opens a new ("special") activity, there's
    // no need to create an artificial back stack.
    PendingIntent resultPendingIntent =
           PendingIntent.getActivity(
                   this,
                   0,
                   resultIntent,
                   PendingIntent.FLAG_UPDATE_CURRENT
           );
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(1,mBuilder.build());


    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 07);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, resultPendingIntent);  //set repeating every 24 hours
    

更新代码:

class 主要 Activity:

PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, new Intent(this, NotificationService.class), PendingIntent.FLAG_CANCEL_CURRENT );

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 15);
    calendar.set(Calendar.MINUTE, 20);
    calendar.set(Calendar.SECOND, 00);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, servicePendingIntent);  //set repeating every 24 hours
    

class 通知服务:

public class NotificationService extends IntentService {

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public NotificationService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.d("NotificationService", "onHandleIntent");

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(this.getText(R.string.notificationMessage))
                        .setContentText("")
                        .setAutoCancel(true);

        Intent resultIntent = new Intent(this, MainActivity.class);

        // Because clicking the notification opens a new ("special") activity, there's
        // no need to create an artificial back stack.
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);

        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(1,mBuilder.build());



    }

}

您的 PendingIntent 指向您的 activity。您只需要改用 PendingIntent.getService,并让此服务显示您的通知。

您正在将 activity 的未决意图传递给警报管理器。您需要将通知代码移至服务(我建议使用 IntentService)。

然后您可能需要创建一个 运行 服务的待定意图,并将该待定意图传递给警报管理器。这样每次闹钟响起时,您的通知代码就是 运行.

PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, new Intent(this, NotificationService.class));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, servicePendingIntent);