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?

  • How to Measure TextView Height Based on Device Width and Font Size?
  • How to check gravity flags in a custom Android View?
  • Convert view to bitmap on Android
  • Android-java- How to sort a list of objects by a certain value within the object
  • Espresso match first element found when many are in hierarchy
  • Desiging an Android App with Offline Sync. Should I use SyncAdapter?
  • Related posts:

    PercentRelativeLayout, how to set the height programmatically
    Custom Class Loading in Dalvik with Gradle (Android New Build System)
    How to put a watch (see how a variable is modified) in Android Studio?
    How to center an ImageView in a LinearLayout?
    I want a progress circle instead of progress dialog
    java.lang.SecurityException: BLUETOOTH permission crash. (Samsung devices only)
  • Android keyboard next button issue on EditText
  • Multiple and single choice test
  • Should I use AsyncTask or IntentService for my application?
  • How to make SearchView always expanded in android?
  • Comparing files in Android Studio
  • Android: How to measure total height of ListView
  • 3 Solutions collect form web for “Service that runs every minute”

    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

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

    Do it like this

        private void ping() {
        try {
            //Your code here or call a method
        } catch (Exception e) {
            Log.e("Error", "In onStartCommand");
            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;
        }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.