如何每一分钟后运行广播接收器?

编程入门 行业动态 更新时间:2024-10-22 11:22:27
本文介绍了如何每一分钟后运行广播接收器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我开发一个应用程序来监控每一分钟后网络。我使用一个BroadcastReceiver这一点。

I am developing an application to monitor the network after every minute. i am using a BroadcastReceiver for this.

我要每一分钟后执行广播接收器。

I want to execute the BroadcastReceiver after every minute.

我该怎么办呢?我可以在广播接收器使用视频下载()?

How can I do it? can i use Thread.sleep() in BroadcastReceiver?

什么情况下可以继续保持运行Android广播接收器?

Is it okay to continuously keep running BroadcastReceiver in android?

推荐答案

BroadcastReceievers被设计成只有当一些广播接收(系统广播或用户定义的广播)运行。如果您想运行一些code的每一分钟,你可以创建一个服务,并安排它使用报警管理器每分钟运行。您可以使用警报管理开始从您广播接收机的服务,它将运行的每一分钟。

BroadcastReceievers are designed to run only when some broadcast is received (System broadcast or user defined broadcast). In case you want to run some code every minute, you can create a service and schedule it for every minute run using an Alarm Manager. You can start the service from your broadcast receiver using alarm manager and it will run every minute.

在你的广播接收器,使用code的onRecieve()方法类似如下:

In the onRecieve() method of your broadcast receiver, use code similar to the below given:

PendingIntent service = null; Intent intentForService = new Intent(context.getApplicationContext(), YourService.class); final AlarmManager alarmManager = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); final Calendar time = Calendar.getInstance(); time.set(Calendar.MINUTE, 0); time.set(Calendar.SECOND, 0); time.set(Calendar.MILLISECOND, 0); if (service == null) { service = PendingIntent.getService(context, 0, intentForService, PendingIntent.FLAG_CANCEL_CURRENT); } alarmManager.setRepeating(AlarmManager.RTC, time.getTime() .getTime(), 60000, service);

更多推荐

如何每一分钟后运行广播接收器?

本文发布于:2023-11-24 09:05:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接收器   分钟后

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!