2012-12-20 6 views
1

서버에서 파일을 다운로드하는 서비스를 시작하려고합니다. 문제는 BroadcastReceiver를 사용하여 DownloadManager의 리턴을 관리 할 때 시작됩니다. MainActivity에 붙여 넣기를 복사하면 작동하지만 서비스에서는 오류가 발생합니다. 내 목표는 파일 (비디오)를 다운로드하고 다음 다운로드를 마친 후 첫 번째 파일 만 다운로드 한 다음 오류를 던집니다.BroadcastReceiver in service

내 클래스 : 답장을

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) 

감사 :

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)); 
    } 
} 

로그 캣, 나는 오류를 이해 해달라고.

+1

"unregisterReceiver()에 대한 호출이 누락 되었습니까?" – Egor

+0

등록을 취소 한 것 같습니다. 파일을 다운로드 한 후 ... AsynTask 함수의 doinbackground에서 AsyncTask를 시도해 볼 수 있습니다. 코드를 구현하면 비디오 파일을 다운로드해야합니다. – itsrajesh4uguys

+0

그것은 작동하지 않습니다 나는 대답을 Blundell의 솔루션을 시도합니다. –

답변

0

IntentService은 요청 사이에서 중지됩니다.

당신이

, 당신의 Activity에 수신기를 이동 onResume에 등록하고 onPause에서 등록 해제 "가 누출"그래서 당신은 당신이 등록을 취소 결코 서비스에 수신기를 등록합니다. service.It 나쁜 생각 입니다 내부

그런 다음 레지스터가 방송을 수신 할 때마다 당신의 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

onHandleIntent()는 지정된 인 텐트가 수신 될 때마다 호출됩니다. 따라서 BroadcastReceiver를 여러 번 등록하십시오.

작업을 마쳤 으면 unregisterReceiver를 호출해야하며, 서비스가 생성 될 때 BroadcastReceive를 등록하거나 onHandleIntent가 트리거되지 않을 때 등록하십시오.

+0

그것은 작동하지 않습니다 나는 대답을 Blundell의 솔루션을 시도합니다. –

0


내 생각에 의도를 보내 넣어 또는 방송 수신기를 등록하지 말아입니다
넓은 캐스트 수신자로부터 서비스를 시작할 수 있습니다.
브로드 캐스트 리시버를 서비스에 등록하면 등록을 취소해야합니다. 더 복잡합니다.