下载失败时不会收到 android.intent.action.DOWNLOAD_COMPLETE

Ant*_*111 5 java android broadcastreceiver android-intent android-download-manager

我有注册到 android.intent.action.DOWNLOAD_COMPLETE 的接收器。

AndroidManifest.xml

       <uses-permission android:name="android.permission.INTERNET" />
       <receiver
            android:name="com.myapp.listener.DownloadListenerService"
            android:exported="true"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
            </intent-filter>
        </receiver>
Run Code Online (Sandbox Code Playgroud)

这是我的接收器 DownloadListenerService.java:

package com.myapp.listener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class DownloadListenerService extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("MYAPP","Start DownloadListenerService");
        //some code
    }
}
Run Code Online (Sandbox Code Playgroud)

仅当下载完成时,我才会在 logcat 消息中看到“启动 DownloadListenerService”。但是,如果下载管理器中的下载状态不成功,我在 logcat 中看不到任何消息。我如何才能发现我的下载未下载?

Fay*_*Zan -1

尝试添加一条if else语句

public void onReceive(Context context, Intent intent) {
    if (context != null){
        Log.d("MYAPP","Start DownloadListenerService");
        //some code
    }else{
        Log.d("MYAPP","Fail DownloadListenerService");
        //some code
    }
 return;
}
Run Code Online (Sandbox Code Playgroud)

编辑:我认为即使您的下载失败,这也会返回 true,所以也许可以(context != null)用其他东西代替?或许(file != null)