首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >AlarmManger setRepeating停止运行

AlarmManger setRepeating停止运行
EN

Stack Overflow用户
提问于 2018-06-10 03:52:15
回答 1查看 131关注 0票数 0

我想每小时发出一条通知。

我在AlarmManager服务中使用setRepeating,问题是当我关闭我的应用程序时,管理器不会广播到我的BroadcastReceiver。

我的BroadcastReceiver:

代码语言:javascript
复制
public class MyReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        Notification notification = new NotificationCompat.Builder(context, "notify_001")
                .setContentTitle("URL Database")
                .setSmallIcon(R.drawable.ic_launcher)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setContentText("You didn't use the URLDatabase app for a while.\nYour urls feel lonely.")
                .setContentIntent(pendingIntent).build();

        NotificationManager manager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel channel = new NotificationChannel("notify_001",
                    "URLDatabaseNotification",
                    NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }

        manager.notify(0, notification);
        Log.i("URLDatabase", "Received");
    }
}

我的AlarmManager代码(我在我的活动的onCreate中使用了这个):

代码语言:javascript
复制
calendar = Calendar.getInstance();
intent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am = (AlarmManager) this.getSystemService(ALARM_SERVICE);

am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60, pendingIntent);

我是否需要创建在后台工作的自定义服务?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50777973

复制
相关文章

相似问题

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