Android提醒!

时间:2011-05-26 13:22:40

标签: android notifications

我想询问哪些服务以及如何在android中使用提醒...让我们说:从现在起10分钟后,在通知栏中显示通知...

感谢您的回答

3 个答案:

答案 0 :(得分:9)

显然你应该使用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);

其中PERIOD是你应该在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);
}

享受。

答案 1 :(得分:2)

您应该使用AlarmManager。有了它,您可以安排交付意图。创建一个BroadcastReceiver来获取它并显示通知。

答案 2 :(得分:1)

我想这样的事情,你在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);