Java BroadcastReceiver 未收到下载完成操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18789246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BroadcastReceiver not receiving download complete action
提问by John Roberts
I am trying to capture download complete events, but my BroadcastReceiver is not receiving them. Here is the receiver:
我正在尝试捕获下载完成事件,但我的 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();
}
}
}
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?
有谁看到有什么问题吗?
采纳答案by Simon
- Use full package name for you receiver like
com.example.DownloadListenerService
- Add
android:exported="true"
BroadcastReceiver
can receive messages from sourcesoutside
its application. Change the name of the
Action
in theintent-filter
toandroid.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" />
- 为您的接收器使用完整的包名称,例如
com.example.DownloadListenerService
- 添加
android:exported="true"
BroadcastReceiver
可以outside
从其应用程序的来源接收消息。 更改的名称
Action
在intent-filter
以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 usingregisterReceiver(@Nullable BroadcastReceiver receiver,IntentFilter filter);
仅当使用从您的应用程序注册时才会触发接收器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);
回答by Kylar
I think the action name in your XML is wrong. The docs state that the correct one is: android.intent.action.DOWNLOAD_COMPLETEnot DownloadManager.ACTION_DOWNLOAD_COMPLETE- you need to use the constant, not the Java form.
我认为您的 XML 中的操作名称是错误的。文档指出正确的是:android.intent.action.DOWNLOAD_COMPLETE而不是DownloadManager.ACTION_DOWNLOAD_COMPLETE- 您需要使用常量,而不是 Java 形式。
<receiver android:name=".DownloadListenerService" >
<intent-filter>
<action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
回答by user755
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);
回答by AndroidLad
I think you are calling DownloadManger service from the IntentService/Service. If so remove it from there and put it into activity.
我认为您正在从 IntentService/Service 调用 DownloadManger 服务。如果是这样,请将其从那里移除并投入使用。
add permission in android manifest
在android清单中添加权限
<uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />
回答by PKSNAP
<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>