我嘗試啓動服務以從服務器下載文件。當我使用BroadcastReceiver管理DownloadManager的返回時,問題就開始了,如果我在我的MainActivity中複製粘貼,它會起作用,但是在Service中會引發錯誤。我的目標是下載一個文件(視頻),當它完成下載下一個,但它只下載第一個,然後拋出錯誤。正在服務的BroadcastReceiver
我的類:
public class VideosDownloader extends IntentService {
public VideosDownloader() {
super("VideosDownloader");
}
private boolean download = true;
private final String SERVER_URL =
"http://127.0.0.1/42de2533d3b2776e456d62cd0fc3a101/";
private SharedPreferences preferenceManager;
final String strPref_Download_ID = "VIDEOS_DOWNLOAD_ID";
private long enqueue;
private DownloadManager manager;
private int count = 0;
private int count_max = 6;
@Override
protected void onHandleIntent(Intent intent) {
Bundle b = intent.getExtras();
preferenceManager = PreferenceManager.getDefaultSharedPreferences(this);
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
// THROW THE FIRST TIME:
DownloadManager.Request request=new DownloadManager.Request(
Uri.parse(SERVER_URL + "video" + count + ".mp4"));
request.setDescription("");
request.setTitle("Downloading");
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, "video" + count + ".mp4");
request.setShowRunningNotification(true);
enqueue = manager.enqueue(request);
Editor PrefEdit = preferenceManager.edit();
PrefEdit.putLong(strPref_Download_ID, enqueue);
PrefEdit.commit();
count++;
BroadcastReceiver receiver = new BroadcastReceiver() {
private int progress = 0;
@Override
public void onReceive(Context context, Intent intent) {
if (count < count_max) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(SERVER_URL + "video" + count + ".mp4"));
request.setDescription("");
request.setTitle("Downloading...");
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, "video" + count
+ ".mp4");
request.setShowRunningNotification(true);
enqueue = manager.enqueue(request);
Editor PrefEdit = preferenceManager.edit();
PrefEdit.putLong(strPref_Download_ID, enqueue);
PrefEdit.commit();
Log.d("ENQUEUE", "ENQUEUE: " + enqueue);
count++;
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
}
logcat的,我不明白的錯誤:
12-20 10:17:06.859: E/ActivityThread(12324): Service com.example.downloadtest.VideosDownloader has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
12-20 10:17:06.859: E/ActivityThread(12324): android.app.IntentReceiverLeaked: Service com.example.downloadtest.VideosDownloader has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
12-20 10:17:06.859: E/ActivityThread(12324): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:756)
12-20 10:17:06.859: E/ActivityThread(12324): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:551)
12-20 10:17:06.859: E/ActivityThread(12324): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:836)
12-20 10:17:06.859: E/ActivityThread(12324): at android.app.ContextImpl.registerReceiver(ContextImpl.java:823)
12-20 10:17:06.859: E/ActivityThread(12324): at android.app.ContextImpl.registerReceiver(ContextImpl.java:817)
12-20 10:17:06.859: E/ActivityThread(12324): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
12-20 10:17:06.859: E/ActivityThread(12324): at com.example.downloadtest.VideosDownloader.onHandleIntent(VideosDownloader.java:132)
12-20 10:17:06.859: E/ActivityThread(12324): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
12-20 10:17:06.859: E/ActivityThread(12324): at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 10:17:06.859: E/ActivityThread(12324): at android.os.Looper.loop(Looper.java:138)
12-20 10:17:06.859: E/ActivityThread(12324): at android.os.HandlerThread.run(HandlerThread.java:60)
感謝您的答覆。
因此,「您是否缺少對unregisterReceiver()的調用?」 – Egor
看起來你錯過了取消註冊。下載文件後......並且您可以嘗試一個AsyncTask for this ...在Asynctask函數的doinbackground中,您應該實現代碼下載視頻文件。 – itsrajesh4uguys
它不工作我會嘗試解決布倫德爾謝謝你的答覆。 –