简体   繁体   English

Android:如何创建提醒(例如Google Keep提醒)

[英]Android: How to create a reminder such as google keep reminder

I want to add a reminder to my android app such as Google Keep app reminder for notes, as you know in Gkeep you can set reminder for your notes by set date (Today,Tomorrow,Next same day or Pick a date from Calendar) and set time (Morning, Afternoon,Evening, Night or pick a time from Clock). 我想在我的Android应用中添加提醒,例如Google Keep应用中的记事提醒,如您在Gkeep中所知,您可以按设置日期(今天,明天,下一天或从日历中选择日期)设置记事提醒。设置时间(早上,下午,晚上,晚上或从时钟中选择时间)。 Any help? 有什么帮助吗?

I found my answer! 我找到了答案! and share it with you because i think its nothing to do with Mechanical Turk :)). 并与您分享,因为我认为与Mechanical Turk无关:))。 in MainActivity: 在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));

and the AlarmReciver class : 和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());
    }
}

And to cancel the reminder : 并取消提醒:

if (alarmManager!= null) {
                alarmManager.cancel(PendingIntent.getBroadcast(MainActivity.this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM