猿问

如何在Android中设置提醒?

我正在尝试为Android构建日历应用。我被打中间了。我已设法从用户检索时间和任务等信息。


我不知道如何将其添加到android事件中。有类似的setEvent东西吗?


慕码人2483693
浏览 570回答 3
3回答

炎炎设计

您可以在带有通知机制的coop中使用AlarmManager,如下所示:Intent intent = new Intent(ctx, ReminderBroadcastReceiver.class);PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);AlarmManager am = (AlarmManager) ctx.getSystemService(Activity.ALARM_SERVICE);// time of of next reminder. Unix time.long timeMs =...if (Build.VERSION.SDK_INT < 19) {&nbsp; &nbsp; am.set(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent);} else {&nbsp; &nbsp; am.setExact(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent);}它开始报警。public class ReminderBroadcastReceiver extends BroadcastReceiver {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onReceive(Context context, Intent intent) {&nbsp; &nbsp; &nbsp; &nbsp; NotificationCompat.Builder builder = new NotificationCompat.Builder(context)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setSmallIcon(...)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setContentTitle(..)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setContentText(..);&nbsp; &nbsp; &nbsp; &nbsp; Intent intentToFire = new Intent(context, Activity.class);&nbsp; &nbsp; &nbsp; &nbsp; PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentToFire, PendingIntent.FLAG_UPDATE_CURRENT);&nbsp; &nbsp; &nbsp; &nbsp; builder.setContentIntent(pendingIntent);&nbsp; &nbsp; &nbsp; &nbsp; NotificationManagerCompat.from(this);.notify((int) System.currentTimeMillis(), builder.build());&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Android
我要回答