0

I have an application in which I have created a service MyService.class Now MyService.class is tied to my activity using bindService() but I want my service to run in the background even if activity destroys itself.

So I started the service and then bind it like below:

private void doBindService() {
  if (!isServiceBound){
    Log.d(LOG_TAG, "Binding Service...");
    if (mBtAdapter != null && mBtAdapter.isEnabled()){
      Intent intent = new Intent(MyActivity.this, MyService.class);
      startService(intent);
      bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }
  }
}

In MyActivity's onDestroy method I am unbinding the service

Now my service is running smoothly until the connection with the remote device breaks. I want to pause/sleep/stop this service if connection breaks, then after every 60 seconds it tries to start the service/connection.

I have tried this but doesn't work.

private void stopService() {
    doUnbindService();
    stopService(new Intent(MyActivity.this, MyService.class));
    startService(new Intent(MyActivity.this, MyService.class));
}

Please any help will be much appreciated. Thanks in advance!!!

AND_SUN
  • 97
  • 2
  • 10
  • If you want the service to keep running in the background after the activity is destroyed, you need to use `startService()` not `bindService()`. A bound service will stop when all of its clients unbind from it. – Karakuri Aug 28 '15 at 15:35

2 Answers2

0

Try this

  • create a method that perform the Operation you want
  • create a Thread or Runnable class
  • Call the Helper method you created in the run() of the Thread or Runnable
  • Inside the Service onStartCommand start the Thread if connection is available
  • thread.wait/sleep if no connection
Want2bExpert
  • 527
  • 4
  • 11
0

I'm not sure I have understanding your request.

With this solution you can play, pause and stop service. And the service do some work every 60seconds

public class MyService extends Service {

    public static boolean testConnexion;

    private Timer timer;

    @Override
    public void onCreate() {
        super.onCreate();
        timer = null;
        testConnexion = true;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (timer != null) {
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    if (testConnexion) {
                        //StartConnexion
                    }
                }
            }, 1000, 60000);
        }

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}

In Any activity To start or stop service. (Your can call startService many times as you want, there will be just one who will run.)

if (v.getId() == R.id.startService) {
            Intent intent = new Intent(this, MyService.class);
            startService(intent);
        } else if (v.getId() == R.id.stopService) {
            Intent intent = new Intent(this, MyService.class);
            stopService(intent);
        }

To pause action (but not kill the service)

MyService.testConnexion = false;

And for restarting

MyService.testConnexion = true;

Your service is not related to your activity. If your killing your activity, your service continues to run.

I hope this can help you

Anthone
  • 2,156
  • 21
  • 26