0


MainActivity starts my TestService in onCreate and bind in onStart method. AsyncThread is started in TestService onStartCommand. bind and unbind methods are called in correct sequences. All things work perfectly, absolutely no issue :).

Issue starts from here: If MainActivity is terminated then running Async thread is also stopped w/o any interrupt exception but TestService is still running which I can check at Running Application Setting. Please help me to find out why thread stops working.

PS: I cross checked with Thread/Handler but same result.

MainActivity and Service code are here:

public class MainActivity extends Activity implements IServiceInterface {

boolean mBound = false;
TestService mTestService = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    Intent serviceIntent = new Intent(this, TestService.class);
    startService(serviceIntent);

}

@Override
protected void onStart() {
    super.onStart();
    // Bind to LocalService
    NSLogger.i("service binding....");
    Intent intent = new Intent(this, TestService.class);
    bindService(intent, mBindConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
    super.onStop();
    if (mBound) {
        NSLogger.i("service unbinding....");
        unbindService(mBindConnection);
        mBound = false;
    }

}

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mBindConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        NSLogger.i("UI is connected to service");
        LocalBinder binder = (LocalBinder) service;
        mTestService = binder.getTestService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        NSLogger.i("UI is disconnected from service");
        mBound = false;
        mTestService = null;
    }
};

}


public class TestService extends Service implements Runnable, Handler.Callback{

 // Binder given to inproc clients
private final IBinder mBinder = new LocalBinder();

@Override
public IBinder onBind(Intent intent) {
    mUIIsBound = true;
    return mBinder;
}

public boolean onUnbind (Intent intent) {
    mUIIsBound = false;
    NSLogger.i("unbind service");
    return true;
}


public class LocalBinder extends Binder {
    TestService getTestService() {
        return TestService.this;
    }

}

public void onDestroy() {
    //NEVER CALLED.
}

public int onStartCommand(Intent intent2, int flags, int startId) {
    NSLogger.i("TestService start!");
    new DownloadConfiguration().execute();
    return START_STICKY;
}

private class DownloadConfiguration extends AsyncTask<Void, Integer, Boolean> {

    @Override
    protected Boolean doInBackground(Void... params) {
         try {
             NSLogger.i("before sleep");
             Thread.sleep(5000);
             NSLogger.i("After sleep");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return true;
    }
}
Sharky
  • 76
  • 6
Andy
  • 338
  • 3
  • 10
  • 1
    If a process is terminated, all threads stop as they cease to exist. But if Android thinks a service still has work to do, it may re-create that in a new process. If this happens, there will be evidence in logcat and you will see that the pid has changed. There may also not be a perfect correlation between the running applications list and what processes currently exist. – Chris Stratton Jan 05 '15 at 12:55
  • Thanks Chris! You are genius. Yes PID is changed. Could you please let me know what I should do so that my background service thread should not be stopped even UI is not stopped? – Andy Jan 05 '15 at 13:09
  • 1
    You could try running your `Service` in a different process. That still may not prevent Android from killing it though. – David Wasser Jan 05 '15 at 13:42
  • Thanks David and Chris! Is there not any way to protect thread? I saw, many apps download information/RSS feeds at background and shows the result. Could you please help to figure out how to do like that? – Andy Jan 05 '15 at 14:19
  • It's simply a fact of life that background processes die on Android - whatever is happening in the foreground has priority for resources. What you can do is make your program able to resume work effectively when Android recreates it. – Chris Stratton Jan 05 '15 at 16:23

0 Answers0