我正在Android上构建一个水提醒应用程序,并希望每天安排有限数量的警报,在特定时间(由用户设置)后停止,并在第二天重新启动而无需用户打开应用程序。我该怎么做呢?
我无法看到如何设置重复警报,因为我希望它们在某一点停止并在第二天重新启动,重复警报不会这样做。我如何实现这一目标?
答案 0 :(得分:0)
您可以设置两个警报,第一个是用于提醒的重复警报。 另一个警报用于在特定时间停止第一个警报,然后安排第二天的警报。
答案 1 :(得分:0)
这是我的示例代码。这可能会对你有所帮助,但我不确定。
MainActivity:
public static int NOW_NOTIFICATION = 101;
Intent intent = new Intent(MainActivity.this, NotificationReceiver.class);
intent.putExtra(NotificationReceiver.NOTIFICATION_MESSAGE, "Sample Alert");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, NOW_NOTIFICATION, intent, PendingIntent.FLAG_ONE_SHOT);
cancelNotification(NOW_NOTIFICATION);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (2 * 1000), pendingIntent);
取消通知:
public void cancelNotification(int requestCode) {
try {
Intent notificationIntent = new Intent(getApplicationContext(), NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
NotificationReceiver:
public class NotificationReceiver extends BroadcastReceiver {
public static String NOTIFICATION_MESSAGE = "notificationMessage";
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra(NOTIFICATION_MESSAGE);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(context, EmptyActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
final DateTime dateTime = DateTime.now();
int color = 0xffffaa00;
// int color1 = context.getColor(R.color.notificatinBackgroundColor);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.festi_push_message_small);
builder.setContentIntent(pendingIntent);
builder.setContentTitle("Notification Sample");
builder.setAutoCancel(true);
builder.setContentText(message + ", Current Time : " + dateTime.getHourOfDay() + ":" + dateTime.getMinuteOfHour() + ":" + dateTime.getSecondOfMinute());
builder.setSound(soundUri);
builder.setLights(Color.RED, 1000, 1000);
builder.setColor(color);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(102938, notification);
}
}
DateChangedReceiver:
public class DateChangedReceiver extends BroadcastReceiver {
public static String NOTIFICATION_MESSAGE = "notificationMessage";
public static int NOW_NOTIFICATION = 101;
Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
Intent intentNitification = new Intent(context, NotificationReceiver.class);
intentNitification.putExtra(NotificationReceiver.NOTIFICATION_MESSAGE, "Sample Alert");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context, NOW_NOTIFICATION, intentNitification, PendingIntent.FLAG_ONE_SHOT);
cancelNotification(NOW_NOTIFICATION);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (2 * 1000), pendingIntent);
}
public void cancelNotification(int requestCode) {
try {
Intent notificationIntent = new Intent(context, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
AndroidManifiest:
<receiver android:name=".NotificationReceiver" />
<receiver android:name=".DateChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>