我搜索了很多,但我不知道如何停止按鈕點擊IntentService
。我有一個按鈕,啓動IntentService
然後progressDialog
顯示通過ResultReceiver
我得到數據回來。這個應用程序搜索和顯示設備的照片,所以我認爲IntentService
是正確的方式來做到這一點。但是我想在取消按鈕上停止它。這是否有一些例子或好的做法?如何停止IntentService
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);
}
http://stackoverflow.com/questions/12112998 /停止意圖服務從活動 – Sangeeta