Java – the broadcastreceiver did not receive the action of completing the download
•
Java
I'm trying to capture download completion events, but my broadcastreceiver doesn't receive them This 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 list:
<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>
Did anyone see anything wrong?
Solution
1 -) use the full package name for your recipient, for example: com example. DownloadListenerService
2 -) add Android: exported = "true" the broadcast receiver can receive messages from sources other than its application
3 -) change the action name in the intention filter to "Android. Intent. Action. Download_complete"
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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" />
</application>
If the request comes from your application, you only receive the broadcast, so run this code in your application to see if the recipient is triggered
DownloadManager dm= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://www.google.com.tw/images/srpr/logo4w.png"));
dm.enqueue(request);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
