How to run BroadcastReceiver after every minute?

末鹿安然 提交于 2019-12-06 13:55:31

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.

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);

You can do it even without AlarmManager

    private void ping() {
    try {
        //Your code here

    } catch (Exception e) {
        e.printStackTrace();
    }
      scheduleNext();
    }

    private void scheduleNext() {
      mHandler.postDelayed(new Runnable() {
        public void run() { ping(); }
      }, 60000);
    }

    public int onStartCommand(Intent intent, int x, int y) {
      mHandler = new android.os.Handler();
      ping();
      return START_STICKY;
    }

Is there a reason that you need a BroadcastReceiver, specifically? If so, the answer is, quite simply, to have some component broadcast the Intent it receives, every minute.

I think that you should have a look at the Alarm Manager. You can program it to fire a PendingIntent at your app, periodically. I'll bet that's what you want.

Anup Cowkur

No. It is absolutely not acceptable to continuously run anything that will drain battery in Android or any other mobile OS if you can help it.

What you should do is use AlarmManager class and fire an Intent every minute or so which in turn will activate a Service in which you can run whatever code you want.

See this answer for an example.

See this thread for more info: Android: How to use AlarmManager

Instead of using BroadcastReceiver i suggest to you to use Service component. It may be possible that your code may require more that 10 seconds to execute, however for BroadcastReceiver you have limit of 10 seconds to execute the code.

Moreover Service works best for background process like downloading,uploading or playing music.

So i suggest you to use execute service rather than BroadcastReceiver.

Instead of BroadcastReceiver. you Should Use AlarmManager to that will start Service at Specific Interval of Time.

you can Get the Sample Source Code from here

Hope it will Help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!