-1

I followed this to Run a Service for Every 5 Min

Till now Its working fine.. But I have added a Intent for Next service in TimeDisplay But its working fine only for the First Time But the second Activity is not running for Every 30 seconds...Its only Working on First Run..

this is MyService

    public class ServMain1 extends Service {

    private static final String TAG = "ServMain1";

    public static final int notify = 30000;
    private Handler mHandler = new Handler();
    private Timer mTimer = null;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Not yet implemented");
    }

   @Override
   public void onCreate() {

       if (mTimer != null) // Cancel if already existed
           mTimer.cancel();
       else
           mTimer = new Timer();   //recreate new
           mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify);

   }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mTimer.cancel();    //For Cancel Timer
        Toast.makeText(this, "Service is Destroyed", Toast.LENGTH_SHORT).show();
    }

    //class TimeDisplay for handling task
    class **TimeDisplay** extends TimerTask {
        @Override
        public void run() {
            // run on another thread
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    // display toast
                    Toast.makeText(ServMain1.this, "ServMain1 : Service is running", Toast.LENGTH_SHORT).show();
                    startService(new Intent(ServMain1.this, ServMain2.class));
                }
            });
        }
    }
}

Here at TimeDisplay I am using this to start second service startService(new Intent(ServMain1.this, ServMain2.class));

How ever I am getting Toast for Every 30 Seconds But Along with that toast I am using a intent is not working...

Its working only for the first time... but I am getting toast of every 30seconds

Can Any one suggest me How to using this kind of activity

Whats Going On
  • 1,379
  • 6
  • 20
  • 49

1 Answers1

1

A service will only run once even after you call startService multiple times.

If you want to keep restarting the service in your handler, you need to first check if it already running, kill it if it is already running and call startService post that.

You can check if the service is running using

private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

And inside your handler make these changes

       mHandler.post(new Runnable() {
    @Override
    public void run() {
        // display toast
        Toast.makeText(ServMain1.this, "ServMain1 : Service is running", Toast.LENGTH_SHORT).show();
        if(!isMyServiceRunning(ServMain2.class)){

            startService(new Intent(ServMain1.this, ServMain2.class));
        } else{
            stopService(ServMain2.class);
            startService(new Intent(ServMain1.this, ServMain2.class));

        }

    }
});
lakshman.pasala
  • 565
  • 6
  • 16