Android:如何创建提醒,如谷歌保持提醒

时间:2015-01-02 19:20:31

标签: java android eclipse

我想在我的Android应用程序中添加提醒,例如Google Keep应用提醒备注,正如您在Gkeep中所知,您可以按设置日期(今天,明天,下一天或挑选)设置提醒。来自日历的日期)和设定时间(早上,下午,晚上,晚上或从时钟中选择一个时间)。 有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

我找到了答案!并与你分享,因为我认为它与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类:

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));
            }