DownloadManager没有收到下载完成操作

时间:2015-07-23 20:29:33

标签: android broadcastreceiver android-download-manager

我有一个意图服务,在后台下载文件,注册广播接收器以监听下载完成,但永远不会进入接收器的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错误失败的权限问题,但我无法弄清楚是什么。

1 个答案:

答案 0 :(得分:1)

我发现了我的问题。您不应该在意向服务中拥有广播接收器,因为意图服务在单独的线程上运行,它将执行onHandleIntent()中的所有内容然后消失。在下载管理器广播下载完成之前,我的意图服务已经消失。