1

I searched a lot but I can't figure how to stop IntentService on button click. I have a button that starts IntentService then progressDialog shows Via ResultReceiver I get data back. This app searches and display photos from the device, so I think IntentService is the right way to do it. But I want to stop it on Cancel button. Is there some example or good practice of this?

MainAcitivity:

@Override
public final void onReceiveResult(final int resultCode, final Bundle resultData) {
    switch (resultCode) {
        case ScanService.STATUS_RUNNING:
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Loading...");
            progressDialog.setCancelable(false);
            progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
//something to stop
                }
            });
            progressDialog.show();

            break;
        case ScanService.STATUS_FINISHED:
            progressDialog.dismiss();
            listOfPictures = resultData.getParcelableArrayList(DATA_KEY);
            break;

        case ScanService.STATUS_ERROR:
            final String error = resultData.getString(Intent.EXTRA_TEXT);
            Toast.makeText(this, error, Toast.LENGTH_LONG).show();
            break;
    }
}

ScanService:

 @Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    return super.onStartCommand(intent, flags, startId);
}

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

@Override
protected void onHandleIntent(final Intent intent) {
    final ResultReceiver scanResultReceiver = intent.getParcelableExtra(RECEIVER_KEY);
    final Bundle bundle = new Bundle();
    scanResultReceiver.send(STATUS_RUNNING, bundle);

 // some work here

    bundle.putParcelableArrayList(DATA_KEY, new ArrayList<>(listOfPictures));
    scanResultReceiver.send(STATUS_FINISHED, bundle);
}
Charuක
  • 12,953
  • 5
  • 50
  • 88
hyrulelink16
  • 389
  • 1
  • 5
  • 17

1 Answers1

1

From outside the service: (e.g button click)

stopService(new Intent(context(), MyIntentService.class));

From inside the service:

stopSelf();

Debugging tools:
In order to check if your Service/IntentService is alive use this ADB command (from command line):

adb shell service list |grep <my.app.packagename>
Nir Duan
  • 6,164
  • 4
  • 24
  • 38
  • is there a way to check if the service has stopped prematurely or finished completely? – hyrulelink16 Dec 04 '16 at 10:04
  • Yes, the service will call stopSelf() when onHandleIntent() is finished, you can add a log at the end of onHandleIntent() and see if this log is written or not, if not the service was stopped prematurely. – Nir Duan Dec 04 '16 at 11:00
  • I added stopService(myintent); in onClick listener ,and stopSelf() in ondestroy of service. At the end of the handleIntent Log is being logged anyways. I tried it and it doesnt stop the service. – hyrulelink16 Dec 04 '16 at 11:33