我想取消我的 Android 应用程序中的 AsyncTask。关键是,我的 AsyncTask 不包含循环,所以我不能使用 break。
这是我的 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;
}
我可以在这里做什么?
最佳答案
AsyncTask.cancel(boolean mayInterruptIfRunning)
成功了。
或从任务内部:
cancel(true);
关于java - 取消无循环的AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32969250/