BroadcastReceiver not receiving download complete action

落爺英雄遲暮 提交于 2019-12-28 04:07:14

问题


I am trying to capture download complete events, but my BroadcastReceiver is not receiving them. Here is the receiver:

public class DownloadListenerService extends BroadcastReceiver {        
    @Override
    public void onReceive(final Context context, Intent intent) {
        System.out.println("got here");
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = settings.edit();

        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            String downloadPath = intent.getStringExtra(DownloadManager.COLUMN_URI);
            editor.putString("downloadPath", downloadPath);
            editor.commit();
        }
    }
}

Here is the manifest:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    <receiver 
        android:name="com.example.alreadydownloaded.DownloadListenerService" 
        android:exported="true">
        <intent-filter>
            <action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
        </intent-filter>
    </receiver>
 </application>

Anyone see what's wrong?


回答1:


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



回答2:


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>



回答3:


  <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>



回答4:


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" />



回答5:


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


来源:https://stackoverflow.com/questions/18789246/broadcastreceiver-not-receiving-download-complete-action

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