[英]BroadcastReceiver not receiving download complete action
I am trying to capture download complete events, but my BroadcastReceiver is not receiving them. 我正在尝试捕获下载完成事件,但是我的BroadcastReceiver没有收到它们。 Here 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 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? 有人知道怎么了吗?
com.example.DownloadListenerService
为您的接收者使用完整的程序包名称,例如com.example.DownloadListenerService
android:exported="true"
BroadcastReceiver
can receive messages from sources outside
its application. 添加android:exported="true"
BroadcastReceiver
可以从其应用程序outside
的来源接收消息。 Change the name of the Action
in the intent-filter
to android.intent.action.DOWNLOAD_COMPLETE
将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" />
The receiver only will be triggered if was registered from your application using registerReceiver(@Nullable BroadcastReceiver receiver,IntentFilter filter);
只有使用 registerReceiver(@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);
I think the action name in your XML is wrong. 我认为您XML中的动作名称是错误的。 The docs state that the correct one is: android.intent.action.DOWNLOAD_COMPLETE not DownloadManager.ACTION_DOWNLOAD_COMPLETE - you need to use the constant, not the Java form. 文档指出正确的是: 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>
<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>
I think you are calling DownloadManger service from the IntentService/Service. 我认为您正在从IntentService / Service调用DownloadManger服务。 If so remove it from there and put it into activity. 如果是这样,请将其从那里删除并投入使用。
add permission in android manifest 在android清单中添加权限
<uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.