[英]How to implement a background thread using java.util.concurrent package?
這是我首先使用的代碼,但在最新的 Android 版本中不推薦使用AsyncTask
類,因此它沒有響應,然后我使用了Thread
類,但該類也不起作用。 我想要與使用AsyncTask
類獲得的結果相同的結果。 我知道我必須使用 java.util.concurrent 包的一些執行程序類,但不知道哪個以及如何使用它。 請幫我解決這個問題。
private static final String USGS_REQUEST_URL =
"https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-05-02&minfelt=50&minmagnitude=5";
EarthquakeAsyncTask task = new EarthquakeAsyncTask();
task.execute(USGS_REQUEST_URL);
private class EarthquakeAsyncTask extends AsyncTask<String, Void, Event> {
@Override
protected Event doInBackground(String... urls) {
// Perform the HTTP request for earthquake data and process the response.
Event result = Utils.fetchEarthquakeData(urls[0]);
return result;
}
@Override
protected void onPostExecute(Event result) {
// Update the information displayed to the user.
updateUi(result);
}
}
private static final String USGS_REQUEST_URL =
"https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-05-02&minfelt=50&minmagnitude=5";
earthquakeRunnable runnable = new earthquakeRunnable(USGS_REQUEST_URL);
runnable.start();
private class earthquakeRunnable extends Thread{
String urls;
earthquakeRunnable(String url){
this.urls = url;
}
@Override
public void run() {
// Perform the HTTP request for earthquake data and process the response.
Event result = Utils.fetchEarthquakeData(urls);
// Update the information displayed to the user
updateUi(result);
}
}
以下是如何在 Activity/Fragment 中使用 ExecutorService 的示例:
// Create some member variables for the ExecutorService
// and for the Handler that will update the UI from the main thread
ExecutorService mExecutor = Executors.newSingleThreadExecutor();
Handler mHandler = new Handler(Looper.getMainLooper());
// Create an interface to respond with the result after processing
public interface OnProcessedListener {
public void onProcessed(Event result);
}
private void processInBg(final String url, final boolean finished){
final OnProcessedListener listener = new OnProcessedListener(){
@Override
public void onProcessed(Event result){
// Use the handler so we're not trying to update the UI from the bg thread
mHandler.post(new Runnable(){
@Override
public void run(){
// Update the UI here
updateUi(result);
// ...
// If we're done with the ExecutorService, shut it down.
// (If you want to re-use the ExecutorService,
// make sure to shut it down whenever everything's completed
// and you don't need it any more.)
if(finished){
mExecutor.shutdown();
}
}
});
}
};
Runnable backgroundRunnable = new Runnable(){
@Override
public void run(){
// Perform your background operation(s) and set the result(s)
Event result = Utils.fetchEarthquakeData(url);
// ...
// Use the interface to pass along the result
listener.onProcessed(result);
}
};
mExecutor.execute(backgroundRunnable);
}
然后,無論您需要在何處觸發后台處理:
processInBg("some_url", true);
根據您的情況,您需要自定義 ExecutorService 的實現以更好地滿足您的需求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.