14

我正在尝试捕获下载完成事件,但我的 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>

任何人都看到有什么问题吗?

4

5 回答 5

17
  • 为您的接收器使用完整的包名称,例如com.example.DownloadListenerService
  • Addandroid:exported="true" BroadcastReceiver可以从其应用程序的源接收消息outside
  • Action将中的名称更改intent-filterandroid.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" />
    

仅当从您的应用程序使用注册时才会触发接收器 registerReceiver(@Nullable BroadcastReceiver receiver,IntentFilter filter);

排队下载的代码:

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);
于 2013-09-13T15:01:20.637 回答
0

我认为您的 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>
于 2013-09-13T14:58:29.290 回答
0
  <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>
于 2016-12-15T11:16:27.983 回答
-1

我认为您正在从 IntentService/Service 调用 DownloadManger 服务。如果是这样,请将其从那里移除并投入活动。

在android清单中添加权限

 <uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />
于 2015-03-13T10:49:25.670 回答
-4

如果下载是基于您的应用程序,那么您需要发送广播吗?

Intent i = new Intent();
i.setAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
sendBroadcast(i);
于 2013-09-13T15:01:41.763 回答