Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加入“;“以后再提醒我”;Android通知中的选项_Android_Mobile Development - Fatal编程技术网

加入“;“以后再提醒我”;Android通知中的选项

加入“;“以后再提醒我”;Android通知中的选项,android,mobile-development,Android,Mobile Development,我正在开发一个简单的水提醒应用程序。剩下要实现的最后一件事是在提醒通知弹出时添加“以后提醒我”选项。我搜索了许多类似的问题和文章,但没有找到解决方案。问题是,我甚至不知道我应该做什么…开始一些活动,或发送一些东西到广播接收器或其他东西。我甚至不知道如何开始尝试不同的方法。如果有人帮助我,我将非常感激!下面是代码。我可以向代码中添加什么来实现此功能 public class ReminderManager { public void createNotificationChannel() {

我正在开发一个简单的水提醒应用程序。剩下要实现的最后一件事是在提醒通知弹出时添加“以后提醒我”选项。我搜索了许多类似的问题和文章,但没有找到解决方案。问题是,我甚至不知道我应该做什么…开始一些活动,或发送一些东西到广播接收器或其他东西。我甚至不知道如何开始尝试不同的方法。如果有人帮助我,我将非常感激!下面是代码。我可以向代码中添加什么来实现此功能

public class ReminderManager {

public void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        Uri soundUri = Uri.parse(Constants.PATH_TO_NOTIFICATION_RINGTONE);
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .build();

        NotificationChannel notificationChannel = new NotificationChannel
                (Constants.NOTIFICATION_CHANNEL_ID, "WaterReminderChannel", NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.setSound(soundUri, audioAttributes);
        notificationChannel.setDescription("Channel for Water Reminder");

        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

public void setReminder() {
    Intent intent = new Intent(context, ReminderBroadcastReceiver.class);
    PendingIntent pendingIntentForBroadcast = PendingIntent.getBroadcast(context, 0, intent, 0);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + MainActivity.reminderTime, pendingIntentForBroadcast);
}



public class ReminderBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent i) {
    if (MainActivity.reminderTime > 0 && !MainActivity.dayFinished) {
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        Uri soundUri = Uri.parse(Constants.PATH_TO_NOTIFICATION_RINGTONE);

        Notification notification = new NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.mipmap.water)
                .setContentTitle("Water Reminder")
                .setContentText("It's time to drink something!")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setSound(soundUri)
                .setDefaults(Notification.DEFAULT_VIBRATE)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(1, notification);
    }
}

如果我理解正确,我相信最简单的处理方法是使用通知“操作按钮”。有关详细信息,请参阅。

如果我理解正确,我相信最简单的处理方法是使用通知“操作按钮”。有关详细信息,请参阅。

谢谢。你的信息帮助我解决了这个问题。我已经做了,我想分享我的代码,因为它非常简单,也许会节省一些人的时间,而不是像我一样搜索很多复杂的解决方案

因此,下面是为用户指定的时间设置警报的代码,在该时间发生后,由于“AlarmManagerCompat.SetExactAndAllowHileId”(适用于较旧和较新的Android版本),即使手机处于空闲状态,广播接收器也会被触发。然后,接收器检查触发动作的方式-打盹或提醒。如果是SNOOZE,它将调用我的自定义提醒管理器类的setReminder()方法来重置报警,然后调用notificationManager.cancelAll()来删除以前的通知横幅。如果是提醒,则调用showNotification()方法,该方法生成通知并将其显示给用户(我不认为需要在AlarmManager设置为触发接收器之前生成通知)

在我的提醒管理器类中,我有createNotificationChannel()方法,这是较新的Android版本所需要的,我在创建MainActivity时调用它。在需要设置提醒的位置(当用户如上所述设置通知和BroadcastReceiver时)调用setRequirement()方法,并如上所述从BroadcastReceiver调用showNotification()方法

public class BroadcastReceiver extends android.content.BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    ReminderManager reminderManager = new ReminderManager(context);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

    if (intent.getAction().equals(Constants.SNOOZE_ACTION)) {
        reminderManager.setReminder();
        notificationManager.cancelAll();
    } else if (intent.getAction().equals(Constants.REMINDER_ACTION)) {
        reminderManager.showNotification();
    }
}
}


}谢谢。你的信息帮助我解决了这个问题。我已经做了,我想分享我的代码,因为它非常简单,也许会节省一些人的时间,而不是像我一样搜索很多复杂的解决方案

因此,下面是为用户指定的时间设置警报的代码,在该时间发生后,由于“AlarmManagerCompat.SetExactAndAllowHileId”(适用于较旧和较新的Android版本),即使手机处于空闲状态,广播接收器也会被触发。然后,接收器检查触发动作的方式-打盹或提醒。如果是SNOOZE,它将调用我的自定义提醒管理器类的setReminder()方法来重置报警,然后调用notificationManager.cancelAll()来删除以前的通知横幅。如果是提醒,则调用showNotification()方法,该方法生成通知并将其显示给用户(我不认为需要在AlarmManager设置为触发接收器之前生成通知)

在我的提醒管理器类中,我有createNotificationChannel()方法,这是较新的Android版本所需要的,我在创建MainActivity时调用它。在需要设置提醒的位置(当用户如上所述设置通知和BroadcastReceiver时)调用setRequirement()方法,并如上所述从BroadcastReceiver调用showNotification()方法

public class BroadcastReceiver extends android.content.BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    ReminderManager reminderManager = new ReminderManager(context);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

    if (intent.getAction().equals(Constants.SNOOZE_ACTION)) {
        reminderManager.setReminder();
        notificationManager.cancelAll();
    } else if (intent.getAction().equals(Constants.REMINDER_ACTION)) {
        reminderManager.showNotification();
    }
}
}

}