Java BroadcastReceiver未接收下载完成操作
我正在尝试捕获下载完整事件,但我的BroadcastReceiver没有接收到它们。这是收信人:Java BroadcastReceiver未接收下载完成操作,java,android,broadcastreceiver,Java,Android,Broadcastreceiver,我正在尝试捕获下载完整事件,但我的BroadcastReceiver没有接收到它们。这是收信人: public class DownloadListenerService extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { System.out.println("got here");
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>
有人看到哪里出了问题吗?我认为XML中的操作名称是错误的。文档中指出正确的选项是:android.intent.action.DOWNLOAD\u COMPLETE而不是DownloadManager.action\u DOWNLOAD\u COMPLETE-您需要使用常量,而不是Java表单
<receiver android:name=".DownloadListenerService" >
<intent-filter>
<action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
- 为您的接收者使用完整的软件包名称,如
com.example.DownloadListenerService
- Add
android:exported=“true”
可以从其应用程序外部的源BroadcastReceiver
接收消息
- 将
intent过滤器中的
名称更改为Action
android.intent.Action.DOWNLOAD\u 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" />
如果下载基于你的应用程序,那么你需要发送广播吗Intent i = new Intent(); i.setAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE); sendBroadcast(i);
我想您是从IntentService/service调用DownloadManger服务的。如果是这样,将其从那里移除,并将其投入活动 在android清单中添加权限<uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />
正如Simon在下面提到的,您的接收器的路径也是错误的。我也改变了这一点。还是不走运。你把下载列表服务的完整路径放进去了吗?像com.foo.DownloadListenerService?尝试将android:enabled=“true”添加到您的定义中。我用动作XML更新了答案。这也不起作用。我将用我的新代码更新我的问题。您正在等待系统发送下载完整事件还是您的应用程序。?等待系统。我正在从Gmail下载一个附件作为我的测试。你能尝试在清单中的意向文件管理器之后添加类别吗。在receiver中设置exproted=“true”和android:enabled=“true”。在intent-filter外部添加enable=true。尝试在代码中这样做,仅用于kicks:context.registerReceiver(onComplete,new IntentFilter(DownloadManager.ACTION\u DOWNLOAD\u COMPLETE));这也不起作用。我要用我的新代码更新我的问题。你知道你只会从下载管理器接收你从应用程序请求的下载的广播,对吗?我不知道。是否有任何方法可以捕获应用程序未请求的下载?如果您在外部存储器中下载文件,您还应该使用权限,为什么我们要添加android:exported=“true”。即使相同的应用程序正在启动downlod,是否也有必要?他需要捕获下载完整事件。不发送新的。添加一些解释和答案,说明此答案如何帮助解决当前问题<uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />
<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>