java – BroadcastReceiver没有收到下载完成的动作
发布时间:2020-09-22 02:18:19 所属栏目:Java 来源:互联网
导读:我正在尝试捕获下载完成的事件,但是我的BroadcastReceiver没有收到它们.这是接收器: public class DownloadListenerService extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent)
|
我正在尝试捕获下载完成的事件,但是我的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();
}
}
}
这是清单: <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>
有人看到有什么问题吗? 解决方法1-)为您的接收者使用完整的包名称,例如:com.example.DownloadListenerService2-)添加android:exported =“true”广播接收器可以从其应用程序之外的源接收消息 3-)将意图过滤器中的动作名称更改为“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>
如果请求来自您的应用程序,您只收到广播,因此在您的应用程序中运行此代码,看看是否触发了接收者 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); (编辑:鄂州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
