[英]Multithreading in a Service (Android/Java)
我的應用經常需要下載圖像。 因此,我實現了一個IntentService來提供下載請求。
第一個問題是IntentService按順序並按順序提供下載內容,我希望有一種方法可以確定下載的優先級並同時進行多個下載。
順便說一句,我正在考慮使用(在IntentService中)一個帶有PriorityBlockingQueue的ThreadPoolExecutor或將PriorityBlockingQueue和AsyncTasks組合在一起。
解決此問題的最佳方法是什么? 還有其他合理的可能性嗎?
提前致謝
您使用AsyncTask。...以下代碼片段將AsyncTask用作您的參考:
私有類doSomethingDelayed擴展了AsyncTask {
private int num_runs = 0;
@Override
protected Void doInBackground(Void... gurk) {
Log.v(TAG, "going into postData");
long ms_before = SystemClock.uptimeMillis();
try {
Log.v(TAG, "Time Now is " + ms_before);
postData();
Log.v(TAG, "coming out of postData");
publishProgress(num_runs);
} catch (Exception e) {
System.out.println("--DoSomeThing--" + e.getMessage());
}
return null;
}
@Override
protected void onCancelled() {
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
}
@Override
protected void onProgressUpdate(Integer... num_runs) {
Context context = getApplicationContext();
}
}
}
}
實際上, IntentService
按順序處理所有內容,並在沒有可用的Intent的情況下將其關閉。 我想當您有單獨的線程來處理您的工作時,這將出錯。
您可以做的是使用“常規” Service
並使用AsyncTask
等。完成任務后,可以調用stopSelf()
。
如果要在后台下載小東西,則android.os.AsyncTask是一種處理方法。 有關示例,請訪問http://code.google.com/p/minemessagevibrator/source/browse/trunk/src/com/mine/oauth/MineOAuthAccessActivity.java 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.