2012-12-20 57 views
1

我嘗試啓動服務以從服務器下載文件。當我使用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) 

感謝您的答覆。

+1

因此,「您是否缺少對unregisterReceiver()的調用?」 – Egor

+0

看起來你錯過了取消註冊。下載文件後......並且您可以嘗試一個AsyncTask for this ...在Asynctask函數的doinbackground中,您應該實現代碼下載視頻文件。 – itsrajesh4uguys

+0

它不工作我會嘗試解決布倫德爾謝謝你的答覆。 –

回答

0

an IntentService在請求之間中止。

當你註冊你從來沒有註銷其服務的接收器,讓你「漏呢」

將您的接收器插入Activity,在onResume註冊和註銷。

然後每次你的寄存器接收廣播時發送的意圖,你Service

http://developer.android.com/reference/android/content/BroadcastReceiver.html

Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack.

+0

好吧,我會嘗試它的信息感謝。 –

0

任何時候收到指定的Intent時都會調用onHandleIntent()。因此,您無數次註冊您的BroadcastReceiver。

因此,當您完成操作時,您必須調用unregisterReceiver,或者在創建服務時註冊BroadcastReceive,而不是在觸發onHandleIntent時註冊。

+0

它不工作我會嘗試Blundell謝謝答覆的解決方案。 –

0


我的想法是不要放或者註冊廣播接收器內的service.It是一個壞主意
您可以從廣播接收器啓動服務。
如果您在服務中放入/註冊廣播接收器,那麼您需要取消註冊。它更復雜