广播接收器接收不到下载完成动作 [英] BroadcastReceiver not receiving download complete action
问题描述
我试图捕捉到下载完成的事件,但我的广播接收器不接收他们。这里是接收器:
I am trying to capture download complete events, but my BroadcastReceiver is not receiving them. 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();
}
}
}
下面是明显的:
<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?
推荐答案
1),使用全包名称为您接收器如:com.example.DownloadListenerService
1-) use full package name for you receiver eg: com.example.DownloadListenerService
2-)添加机器人:出口=真正的广播接收器可以在其应用程序之外的接收源的邮件
2-) add android:exported="true" broadcast receiver can receive messages from sources outside its application
3)的意图过滤器改变动作的名称为android.intent.action.DOWNLOAD_COMPLETE
3-) change the name of the action in the intent filter to "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>
您只收到广播请求是否来自您的应用程序,使应用程序运行在你里面这个code,看看是否接收器被触发
you only received broadcast if the request was from your app, so run this code inside you app and see if receiver is triggered
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);
这篇关于广播接收器接收不到下载完成动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!