我想问一下在android中使用哪种服务以及如何使用提醒…假设: 10分钟后在通知栏中显示通知...
谢谢你的回答
发布于 2011-05-26 21:54:26
显然,您应该使用AlarmManager来设置要在给定时间内执行的内容。
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);其中,句点是您执行应该在OnAlarmReceiver中执行的操作的时间。然后,只需在
@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);
}好好享受吧。
发布于 2011-05-26 21:52:53
您应该使用AlarmManager。使用它,您可以计划要交付的意图。创建一个BroadcastReceiver来获取它并显示通知。
发布于 2011-05-26 21:56:38
有些人这样想,我猜,你在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);https://stackoverflow.com/questions/6139182
复制相似问题