簡體   English   中英

DownloadManager沒有收到下載完成操作

[英]DownloadManager doesn't receive download complete action

我有一個意圖服務,可以在后台下載文件,注冊了廣播接收器以偵聽下載完成,但從未進入接收器的onReceive()函數。 該文件似乎已完成下載,我可以在文件瀏覽器中看到它,並從DownloadManager收到狀態為SUCCESS的消息。 在下載成功消息之前,我遇到錯誤Failed to chmod /mnt/internal_sd/../filedownloaded

意向從主要活動onCreate開始:

Intent i = new Intent(context, MyIntentService.class);
startService(i);

意向服務:

public class MyIntentService extends IntentService  {

    DownloadManager downloadManager;

    @Override
    protected void onHandleIntent(Intent intent) {
        IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        registerReceiver(downloadReceiver, filter);
        downloadFile();
    }

    void downloadFile(Uri downloadUri) {
        downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

        DownloadManager.Request request = new DownloadManager.Request(downloadUri);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        request.setAllowedOverRoaming(false);
        request.setTitle("My Andorid App Download");
        request.setDestinationInExternalFilesDir(getApplicationContext(), null, sku + ".apk");

        long downloadNum = downloadManager.enqueue(request);        
    }

    private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {         
            System.out.println("does not get in here.");
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
            Uri u = downloadManager.getUriForDownloadedFile(id);
        }
    };
}

表現:

<service
    android:name="com.example.MyIntentService"
    android:exported="false">

    <intent-filter>
      <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />                 
    </intent-filter>

</service> 

聽起來好像是chmod錯誤失敗導致的權限問題,但我不太清楚是什么原因。

我發現了我的問題。 您不應該在Intent服務中擁有廣播接收器,因為Intent服務在單獨的線程上運行,它將在onHandleIntent()執行所有操作,然后消失。 在下載管理器可以廣播下載完成之前,我的意圖服務已經消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM