0

I am trying to understand the difference bwtettn STICKY and NOT_STICKY intent of a service in onStartCommand(). I created the MainActivity and the Service shown in the code below. I expected when I press the button mBtnStopApp, the onDestroy() wil be called and then I should receive this log ** Log.w(TAG, SubTag.bullet("++++++++ SERVICE IS NOT RUNNING ++++++++")); ** because onStartCommand() returns NOT_STICKY intnet which means, when the App is killed, the service should also be stopped but that never happens.

what happens is, when i press the button mBtnStopApp, the App is finished but i receive the following log: ** Log.w(TAG, SubTag.bullet("++++++++ SERVICE IS RUNNING ++++++++")); **

please let me know why the service is still running despite it returns START_NOT_STICKY and the App was killed

MainActivity

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();
private TextView mTv = null;
private Button mBtnStartStickyService = null;
private Button mBtnStartNonStickyService = null;
private Button mBtnStopApp = null;
private Button mBtnStopNonStickyService = null;
private Button mBtnStopStickyService = null;
private ServicesUtils mServiceUtils = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.w(TAG, SubTag.bullet("@onCreate"));

    this.mServiceUtils = new ServicesUtils(this);
    this.mBtnStartNonStickyService = (Button) findViewById(R.id.btnNonStickyService);
    this.mBtnStartStickyService = (Button) findViewById(R.id.btnStickyService);
    this.mBtnStopApp = (Button) findViewById(R.id.btnStopApp);
    this.mBtnStopNonStickyService = (Button) findViewById(R.id.btnStopNonStickyService);
    this.mBtnStopStickyService = (Button) findViewById(R.id.btnStopStickyService);
    this.mTv = (TextView) findViewById(R.id.tv);

    this.mBtnStartNonStickyService.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ComponentName componentName = startService(setRunningStateForService(NonStickyService.class, "start"));
            Log.i(TAG, "@mBtnStartNonStickyService: componentName: " + componentName);
        }
    });

    this.mBtnStopNonStickyService.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ComponentName componentName = startService(setRunningStateForService(NonStickyService.class, "stop"));
            Log.i(TAG, "@mBtnStopNonStickyService: componentName: " + componentName);
        }
    });
    this.mBtnStopApp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

private Intent setRunningStateForService(Class<NonStickyService> serviceClass, String state) {
    Log.w(TAG, SubTag.bullet("@setRunningStateForService"));
    Intent intent = new Intent(this, serviceClass);
    intent.putExtra("running", state);
    return  intent;
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.w(TAG, SubTag.bullet("@onDestroy"));
    if (this.isNonStickyServiceRunning()) {
        Log.w(TAG, SubTag.bullet("++++++++ SERVICE IS RUNNING ++++++++"));
    } else {
        Log.w(TAG, SubTag.bullet("++++++++ SERVICE IS NOT RUNNING ++++++++"));
    }
}

private boolean isNonStickyServiceRunning() {
    ActivityManager manager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (NonStickyService.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}
}

code:

public class NonStickyService extends Service {

private static final String TAG = NonStickyService.class.getSimpleName();
private HeavyWorkRunnable mHeavyWorkRunnable = null;
private String running = null;

@Override
public void onCreate() {
    super.onCreate();
    Log.w(TAG, SubTag.bullet("@onCreate"));
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.w(TAG, SubTag.bullet("@onStartCommand"));

    running = intent.getStringExtra("running");
    if (running != null && !running.isEmpty() && running.equals("start")) {
        this.StartHeavyWorkThread();
    }
    return Service.START_NOT_STICKY;
}

private void StartHeavyWorkThread() {
    Log.w(TAG, SubTag.bullet("@StartHeavyWorkThread"));
    this.mHeavyWorkRunnable = new HeavyWorkRunnable();
    new Thread(this.mHeavyWorkRunnable).start();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    Log.w(TAG, SubTag.bullet("@onBind"));
    return null;
}

@Override
public void onDestroy() {
    Log.w(TAG, SubTag.bullet("@onDestroy"));
    super.onDestroy();
    this.running = "stop";
}

class HeavyWorkRunnable implements Runnable {
    int counter = 0;
    private Bundle b = null;
    public void run() {
        Log.w(TAG,SubTag.bullet("@HeavyWorkRunnable"));

        while(running.equals("start")) {
            b = new Bundle();
            b.putInt("what", ++counter);
            Log.w(TAG,SubTag.bullet("@HeavyWorkRunnable: counter: " + counter));
            Log.w(TAG,SubTag.bullet("@HeavyWorkRunnable: b.getInt('what'): " + b.getInt("what")));

            Message msg = handler.obtainMessage();
            msg.setData(b);
            handler.sendMessage(msg);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Log.i(TAG,SubTag.bullet("NonStickyService will Stop"));
        stopSelf();
    }
}

android.os.Handler handler = new android.os.Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        int what = msg.getData().getInt("what");
        Log.w(TAG,SubTag.bullet("@handler: what: " + what));
    }
};
}
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • https://stackoverflow.com/questions/17378819/is-the-android-service-still-alive-even-after-the-ondestroy-be-called - refer this link – kiran Biradar Jul 27 '18 at 09:11
  • Possible duplicate of [Is the Android service still alive even after the onDestroy() be called?](https://stackoverflow.com/questions/17378819/is-the-android-service-still-alive-even-after-the-ondestroy-be-called) – kiran Biradar Jul 27 '18 at 09:13

0 Answers0