我想在我的Android應用程序中取消一個AsyncTask。踢球者是,我的AsyncTask不包含循環,所以我不能使用break。取消沒有循環的AsyncTask
這裏是我的AsyncTask.doInBackground():
protected Void doInBackground(String... params) {
String location = params[1];
if(!location.matches("-?[0-9.]*,-?[0-9.]*")) {
try {
location = Util.getLatLngFromMapsQuery(location);
} catch (UnchainedAPIException e) {
setErrorCode(ERROR_GEO);
this.cancel(true);
}
}
UnchainedAPI unchainedAPI = new UnchainedAPI(YELP_KEY, YELP_SECRET, YELP_TOKEN, YELP_TOKEN_SECRET,
FOURSQUARE_ID, FOURSQUARE_SECRET, GOOGLE_PLACES_KEY);
try {
nonChains = unchainedAPI.getUnchainedRestaurants(params[0], location);
} catch (UnchainedAPIException e) {
setErrorCode(ERROR_API);
this.cancel(true);
}
if(nonChains.size() == 0) {
setErrorCode(ERROR_API);
}
return null;
}
我能做些什麼嗎?
根據你想取消什麼?我覺得這種方法會完成並結束,我沒有看到任何阻止線程,你想殺死它或停止執行方法.. – hagrawal
檢查這是一個很好的例子,以正確的方式取消一個asynctask http: //www.quicktips.in/correct-way-to-cancel-an-asynctask-in-android/ –