[英]Android reminder!
I would like to ask which service and how to use to make reminder in android... Let's say: after 10 minutes from now show me notification in notification bar...我想问在android中使用哪个服务以及如何使用提醒...假设:10分钟后在通知栏中显示通知...
Thanks for your answer感谢您的回答
Obviously you should use AlarmManager in order to setup something to be executed in given PERIOD.显然,您应该使用 AlarmManager 来设置要在给定 PERIOD 中执行的内容。
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, OnAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), PERIOD, pi);
where the PERIOD is your time to something that should be executed in OnAlarmReceiver.其中 PERIOD 是您应该在 OnAlarmReceiver 中执行的操作的时间。 And then, just implement method in然后,只需在
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager nm = (NotificationManager);
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.tickerText = "10 Minutes past";
nm.notify(0, notification);
}
Enjoy.享受。
You should useAlarmManager
.您应该使用AlarmManager
。 With it you can schedule an intent to be delivered.有了它,您可以安排要交付的意图。 Create a BroadcastReceiver
to get it and show the notification.创建一个BroadcastReceiver
以获取它并显示通知。
Somethink like this i guess, you start an runnable after 10min and open an notification in the runnable's code.我猜想像这样,你在 10 分钟后启动一个 runnable 并在 runnable 的代码中打开一个通知。
Runnable reminder = new Runnable()
{
public void run()
{
int NOTIFICATION_ID = 1324;
NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon, "message", System.currentTimeMillis());
// Add and AUTO_CANCEL flag to the notification,
// this automatically removes the notification when the user presses it
note.flags |= Notification.FLAG_AUTO_CANCEL;
PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, ActivityToOpen.class), 0);
note.setLatestEventInfo(this, "message", title, i);
notifManager.notify(NOTIFICATION_ID, note);
}
};
Handler handler = new Handler();
handler.postDelayed(reminder, 600000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.