Android:如何创建提醒,例如google keep reminder
Android: How to create a reminder such as google keep reminder
我想在我的 android 应用程序中添加一个提醒,例如 Google Keep 笔记的应用程序提醒,如您所知,在 Gkeep 中,您可以按设定日期(今天、明天)为笔记设置提醒, 下一天或从日历中选择一个日期)并设置时间(早上、下午、晚上、晚上或从时钟中选择一个时间)。
有帮助吗?
我找到答案了!并与您分享,因为我认为它与 Mechanical Turk 无关 :))。
在 MainActivity 中:
final AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
final Intent intentAlarm = new Intent(MainActivity.this, AlarmReceiver.class);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 22);
calendar.set(Calendar.HOUR, 5);
calendar.set(Calendar.AM_PM, Calendar.PM);
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 6);
calendar.set(Calendar.YEAR,2015);
long when = calendar.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP,when, PendingIntent.getBroadcast(MainActivity.this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
和 AlarmReciver class :
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Calendar now = GregorianCalendar.getInstance();
Notification.Builder mBuilder =
new Notification.Builder(context)
.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("my title")
.setContentText("my text");
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
并取消提醒:
if (alarmManager!= null) {
alarmManager.cancel(PendingIntent.getBroadcast(MainActivity.this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}
我想在我的 android 应用程序中添加一个提醒,例如 Google Keep 笔记的应用程序提醒,如您所知,在 Gkeep 中,您可以按设定日期(今天、明天)为笔记设置提醒, 下一天或从日历中选择一个日期)并设置时间(早上、下午、晚上、晚上或从时钟中选择一个时间)。 有帮助吗?
我找到答案了!并与您分享,因为我认为它与 Mechanical Turk 无关 :))。 在 MainActivity 中:
final AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
final Intent intentAlarm = new Intent(MainActivity.this, AlarmReceiver.class);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 22);
calendar.set(Calendar.HOUR, 5);
calendar.set(Calendar.AM_PM, Calendar.PM);
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 6);
calendar.set(Calendar.YEAR,2015);
long when = calendar.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP,when, PendingIntent.getBroadcast(MainActivity.this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
和 AlarmReciver class :
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Calendar now = GregorianCalendar.getInstance();
Notification.Builder mBuilder =
new Notification.Builder(context)
.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("my title")
.setContentText("my text");
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
并取消提醒:
if (alarmManager!= null) {
alarmManager.cancel(PendingIntent.getBroadcast(MainActivity.this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}