Java 如何在几个小时后重复android通知

Java 如何在几个小时后重复android通知,java,android,android-sqlite,android-notifications,Java,Android,Android Sqlite,Android Notifications,我的应用程序需要在一小时后重复一次通知(医疗用途)。由于我的领域是完全相反的,我在编码noob任何帮助将不胜感激。我知道我必须在通知接收器中添加一些内容来重复通知。但每次我尝试重复,应用程序都会崩溃 (这是一个解决现实世界问题的小而独特的想法,我相信我从每个人那里得到了一点帮助) 这是我的主要活动 private NotificationManagerCompat manager; @Override protected void onCreate(Bundle savedInstanceSta

我的应用程序需要在一小时后重复一次通知(医疗用途)。由于我的领域是完全相反的,我在编码noob任何帮助将不胜感激。我知道我必须在通知接收器中添加一些内容来重复通知。但每次我尝试重复,应用程序都会崩溃

(这是一个解决现实世界问题的小而独特的想法,我相信我从每个人那里得到了一点帮助)

这是我的主要活动

private NotificationManagerCompat manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    manager =  NotificationManagerCompat.from(this);
}

public void Shownotification(View v) {

    String title = "You Did it!";
    String message = "Some Text";

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle(title)
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .build();
        manager.notify(0,notification);
}
通知通道

public class App extends Application {

public static final String CHANNEL_1_ID = "channel1";
public static final String CHANNEL_2_ID = "channel2";

@Override
public void onCreate() {
    super.onCreate();

    createNotificationChannels();
}

private void createNotificationChannels() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel1 = new NotificationChannel(
                CHANNEL_1_ID,
                "Channel 1",
                NotificationManager.IMPORTANCE_HIGH
        );
        channel1.setDescription("This is Channel 1");

        NotificationChannel channel2 = new NotificationChannel(
                CHANNEL_2_ID,
                "Channel 2",
                NotificationManager.IMPORTANCE_LOW
        );
        channel2.setDescription("This is Channel 2");

        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(channel1);
        manager.createNotificationChannel(channel2);
    }
}
通知接收器

public class NotificationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String title = "You Did it!";
    String message = "Some Text";

    Notification notification = new NotificationCompat.Builder(context, CHANNEL_1_ID)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle(title)
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, 1);
    Intent i = new Intent("android.action.DISPLAY_NOTIFICATION");
    i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);

}

当上一个触发时,您可以设置新通知。在BroadcastReceive的onReceive方法中。

请您的问题提供完整的。应用程序目前还可以。我希望该通知每1小时重复一次,然后以分钟为单位递增时间。然后显示不起作用的代码,还有崩溃后的堆栈跟踪。好的,等一下更新你需要的是Dhamik谢谢你的回复,但我想在1小时后重复这些