Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service that runs every minute

I have a service that I am wanting to execute a task every minute in the background. It does not need to execute the task whenever the phone is asleep, only when the user is actively using it. I am trying to do this with an IntentService which is set up as follows:

public class CounterService extends IntentService{

    public CounterService() {
        super("CounterService");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       return super.onStartCommand(intent,flags,startId);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Toast.makeText(this, "onhandleintent", Toast.LENGTH_SHORT).show();
        while(true)
        {
            //one minute is 60*1000
            try {
                Thread.sleep(5 * 1000);
                Toast.makeText(getApplicationContext(), "getting app count", Toast.LENGTH_LONG).show();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

Right now to get the functionality working I simply want it to display a toast every 5 seconds, I will change it to one minute later. If I have the while(true) commented out, then the "onhandleintent" message is displayed. However if I have the following code run, neither of the Toasts display. How can I fix this?

like image 812
AggieDev Avatar asked Feb 23 '14 21:02

AggieDev


People also ask

How do I get service to run every 5 minutes?

Create a Timer object and give it a TimerTask that performs the code you'd like to perform. The advantage of using a Timer object is that it can handle multiple TimerTask objects, each with their own timing, delay, etc.

How do I run a Windows service continuously in C#?

Inside the loop, we sleep for 1 second. You'll want to replace this with the work you need to do - monitor proxy settings, etc. Finally, in the OnStop() callback of your Windows Service, you want to signal the thread to stop running. This is easy using the _shutdownEvent .


2 Answers

This will send an intent to your service every minute without using any processor time in your activity in between

  Intent myIntent = new Intent(context, MyServiceReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context,  0, myIntent, 0);

  AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(System.currentTimeMillis());
  calendar.add(Calendar.SECOND, 60); // first time
  long frequency= 60 * 1000; // in ms 
  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), frequency, pendingIntent);           

Adjust MyServiceReceiver.class to match your target service or activity. The documentation provides more details to fine-tune your calls like whether you want exact timing, execution at a specific time of the day ...

like image 58
Android Newbie Avatar answered Nov 07 '22 07:11

Android Newbie


You need to exit the main thread to avoid risking a ANR.

Instead add a Handler

Handler mHandler = new Handler();

...

@Override
protected void onHandleIntent(Intent intent) {
    Toast.makeText(this, "onhandleintent", Toast.LENGTH_SHORT).show();
    mHandler.postDelayed( ToastRunnable, 5000);
    //while(true)
    //{

        //one minute is 60*1000
        //try {
        //    Thread.sleep(5 * 1000);
        //    Toast.makeText(getApplicationContext(), "getting app count",          
        //Toast.LENGTH_LONG).show();
        //} catch (InterruptedException e) {
            // TODO Auto-generated catch block
        //    e.printStackTrace();
        //}
    //}
}
final Runnable ToastRunnable = new Runnable(){
    public void run(){
         Toast.makeText(getApplicationContext(), "getting app count",          
               Toast.LENGTH_LONG).show();
         mHandler.postDelayed( ToastRunnable, 5000);
    }
}
like image 24
jeremyvillalobos Avatar answered Nov 07 '22 07:11

jeremyvillalobos