我正在 try catch 下载完成事件,但我的 BroadcastReceiver 没有收到它们。这是接收器:
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();
}
}
}
这是 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>
有人看到哪里出了问题吗?
最佳答案
- 为您的接收器使用完整的包名称,例如
com.example.DownloadListenerService
- 添加
android:exported="true"
BroadcastReceiver
可以从其应用程序外部
的来源接收消息。 将
intent-filter
中的Action
的名称更改为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" />
仅当使用 registerReceiver(@Nullable BroadcastReceiver receiver,IntentFilter filter);
排队下载的代码:
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);
关于java - BroadcastReceiver 未收到下载完成操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18789246/