首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android提醒!

Android提醒!
EN

Stack Overflow用户
提问于 2011-05-26 21:22:40
回答 3查看 4K关注 0票数 3

我想问一下在android中使用哪种服务以及如何使用提醒…假设: 10分钟后在通知栏中显示通知...

谢谢你的回答

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-26 21:54:26

显然,您应该使用AlarmManager来设置要在给定时间内执行的内容。

代码语言:javascript
复制
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中执行的操作的时间。然后,只需在

代码语言:javascript
复制
@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);
}

好好享受吧。

票数 9
EN

Stack Overflow用户

发布于 2011-05-26 21:52:53

您应该使用AlarmManager。使用它,您可以计划要交付的意图。创建一个BroadcastReceiver来获取它并显示通知。

票数 2
EN

Stack Overflow用户

发布于 2011-05-26 21:56:38

有些人这样想,我猜,你在10分钟后启动一个runnable,并在runnable的代码中打开一个通知。

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6139182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档