Android 如何一次点击按钮设置不同时间的报警?
我想设置2个不同时间的报警,并用一个单击按钮将其保存到sqllite数据库中,第一个报警成功显示为通知,但在第二个报警时,什么也没有发生 这是我添加报警的代码:Android 如何一次点击按钮设置不同时间的报警?,android,android-studio,alarmmanager,Android,Android Studio,Alarmmanager,我想设置2个不同时间的报警,并用一个单击按钮将其保存到sqllite数据库中,第一个报警成功显示为通知,但在第二个报警时,什么也没有发生 这是我添加报警的代码: ContentValues values = new ContentValues(); mHour1 = Integer.parseInt(jam1.getText().toString()); mMinute1 = Integer.par
ContentValues values = new ContentValues();
mHour1 = Integer.parseInt(jam1.getText().toString());
mMinute1 = Integer.parseInt(menit1.getText().toString());
values.put(AlarmReminderContract.AlarmReminderEntry.KEY_TITLE, namaobat.getText().toString());
values.put(AlarmReminderContract.AlarmReminderEntry.KEY_DATE, new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
values.put(AlarmReminderContract.AlarmReminderEntry.KEY_TIME, mHour1 + ":" +mMinute1);
values.put(AlarmReminderContract.AlarmReminderEntry.KEY_KEBERAPA, "satu");
values.put(AlarmReminderContract.AlarmReminderEntry.KEY_ACTIVE, "true");
calendar.set(Calendar.MONTH, --mMonth);
calendar.set(Calendar.YEAR, mYear);
calendar.set(Calendar.DAY_OF_MONTH, mDay);
calendar.set(Calendar.HOUR_OF_DAY, mHour1);
calendar.set(Calendar.MINUTE, mMinute1);
calendar.set(Calendar.SECOND, 0);
long selectedTimestamp = calendar.getTimeInMillis();
Uri newUri = getContentResolver().insert(AlarmReminderContract.AlarmReminderEntry.CONTENT_URI, values);
new AlarmScheduler().setAlarm(getApplicationContext(), selectedTimestamp, newUri);
ContentValues values2 = new ContentValues();
mHour2 = Integer.parseInt(jam2.getText().toString());
mMinute2 = Integer.parseInt(menit2.getText().toString());
values2.put(AlarmReminderContract.AlarmReminderEntry.KEY_TITLE, namaobat.getText().toString());
values2.put(AlarmReminderContract.AlarmReminderEntry.KEY_DATE, new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
values2.put(AlarmReminderContract.AlarmReminderEntry.KEY_TIME, mHour2 + ":" +mMinute2);
values2.put(AlarmReminderContract.AlarmReminderEntry.KEY_KEBERAPA, "dua");
values2.put(AlarmReminderContract.AlarmReminderEntry.KEY_ACTIVE, "true");
calendar.set(Calendar.MONTH, --mMonth);
calendar.set(Calendar.YEAR, mYear);
calendar.set(Calendar.DAY_OF_MONTH, mDay);
calendar.set(Calendar.HOUR_OF_DAY, mHour2);
calendar.set(Calendar.MINUTE, mMinute2);
calendar.set(Calendar.SECOND, 0);
long selectedTimestamp2 = calendar.getTimeInMillis();
Uri newUri2 = getContentResolver().insert(AlarmReminderContract.AlarmReminderEntry.CONTENT_URI, values2);
new AlarmScheduler().setAlarm(getApplicationContext(), selectedTimestamp2, newUri2);
这是我的AlarmScheduler类:
public void setAlarm(Context context, long alarmTime, Uri reminderTask) {
AlarmManager manager = AlarmManagerProvider.getAlarmManager(context);
PendingIntent operation =
ReminderAlarmService.getReminderPendingIntent(context, reminderTask);
if (Build.VERSION.SDK_INT >= 23) {
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime, operation);
} else if (Build.VERSION.SDK_INT >= 19) {
manager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, operation);
} else {
manager.set(AlarmManager.RTC_WAKEUP, alarmTime, operation);
}
public class ReminderAlarmService extends IntentService {
private static final String TAG = ReminderAlarmService.class.getSimpleName();
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
public static PendingIntent getReminderPendingIntent(Context context, Uri uri) {
Intent action = new Intent(context, ReminderAlarmService.class);
action.setData(uri);
return PendingIntent.getService(context, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
}
public ReminderAlarmService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Uri uri = intent.getData();
//Display a notification to view the task details
Intent action = new Intent(this, Reminder_Activity.class);
action.setData(uri);
PendingIntent operation = TaskStackBuilder.create(this)
.addNextIntentWithParentStack(action)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
//Grab the task description
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
String title = "";
String keberapa = "";
try {
if (cursor != null && cursor.moveToFirst()) {
title = AlarmReminderContract.getColumnString(cursor, AlarmReminderContract.AlarmReminderEntry.KEY_TITLE);
keberapa = AlarmReminderContract.getColumnString(cursor, AlarmReminderContract.AlarmReminderEntry.KEY_KEBERAPA);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Pengingat Minum Obat", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Jangan lupa untuk minum "+title+" yang ke"+keberapa+" di hari ini ya! Karena kesembuhanmu sudah di depan mata.");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{0, 100, 100, 100, 100, 100})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(operation)
.setContentTitle("Pengingat Minum Obat")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Jangan lupa untuk minum "+title+" yang ke"+keberapa+" di hari ini ya! Karena kesembuhanmu sudah di depan mata."));
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
这是我的闹钟提醒服务类:
public void setAlarm(Context context, long alarmTime, Uri reminderTask) {
AlarmManager manager = AlarmManagerProvider.getAlarmManager(context);
PendingIntent operation =
ReminderAlarmService.getReminderPendingIntent(context, reminderTask);
if (Build.VERSION.SDK_INT >= 23) {
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime, operation);
} else if (Build.VERSION.SDK_INT >= 19) {
manager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, operation);
} else {
manager.set(AlarmManager.RTC_WAKEUP, alarmTime, operation);
}
public class ReminderAlarmService extends IntentService {
private static final String TAG = ReminderAlarmService.class.getSimpleName();
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
public static PendingIntent getReminderPendingIntent(Context context, Uri uri) {
Intent action = new Intent(context, ReminderAlarmService.class);
action.setData(uri);
return PendingIntent.getService(context, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
}
public ReminderAlarmService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Uri uri = intent.getData();
//Display a notification to view the task details
Intent action = new Intent(this, Reminder_Activity.class);
action.setData(uri);
PendingIntent operation = TaskStackBuilder.create(this)
.addNextIntentWithParentStack(action)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
//Grab the task description
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
String title = "";
String keberapa = "";
try {
if (cursor != null && cursor.moveToFirst()) {
title = AlarmReminderContract.getColumnString(cursor, AlarmReminderContract.AlarmReminderEntry.KEY_TITLE);
keberapa = AlarmReminderContract.getColumnString(cursor, AlarmReminderContract.AlarmReminderEntry.KEY_KEBERAPA);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Pengingat Minum Obat", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Jangan lupa untuk minum "+title+" yang ke"+keberapa+" di hari ini ya! Karena kesembuhanmu sudah di depan mata.");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{0, 100, 100, 100, 100, 100})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(operation)
.setContentTitle("Pengingat Minum Obat")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Jangan lupa untuk minum "+title+" yang ke"+keberapa+" di hari ini ya! Karena kesembuhanmu sudah di depan mata."));
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}