BroadcastReceiver not receiving download complete action

给你一囗甜甜゛ 提交于 2019-11-27 14:23:09
  • Use full package name for you receiver like com.example.DownloadListenerService
  • Add android:exported="true" BroadcastReceiver can receive messages from sources outside its application.
  • Change the name of the Action in the intent-filter to android.intent.action.DOWNLOAD_COMPLETE

        <receiver 
            android:name="com.example.DownloadListenerService"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
            </intent-filter>
        </receiver>
        <uses-permission android:name="android.permission.INTERNET" />
    

The receiver only will be triggered if was registered from your application using registerReceiver(@Nullable BroadcastReceiver receiver,IntentFilter filter);

Code to enqueue Download :

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://www.google.com/images/srpr/logo4w.png"));
dm.enqueue(request);

I think the action name in your XML is wrong. The docs state that the correct one is: android.intent.action.DOWNLOAD_COMPLETE not DownloadManager.ACTION_DOWNLOAD_COMPLETE - you need to use the constant, not the Java form.

<receiver android:name=".DownloadListenerService" >
    <intent-filter>
        <action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
    </intent-filter>
</receiver>
  <receiver
        android:name=".BroadCast_Service.Download_BroadCast"
        android:exported="true">
        <intent-filter android:priority="1099">
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
        </intent-filter>
    </receiver>
AndroidLad

I think you are calling DownloadManger service from the IntentService/Service. If so remove it from there and put it into activity.

add permission in android manifest

 <uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />

If the download is based on your app then you need to send a broadcast?

Intent i = new Intent();
i.setAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
sendBroadcast(i);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!