android.intent.action.DOWNLOAD_COMPLETE在下载失败时未收到

时间:2016-10-18 09:01:34

标签: java android android-intent broadcastreceiver 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>

这是我的reciver 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
    }
}

我只在下载完成后才在logcat消息“Start DownloadListenerService”中看到。但如果下载管理器中的下载获取状态不成功,我在logcat中看不到任何消息。我怎么能发现我的下载没有下载?

1 个答案:

答案 0 :(得分:-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;
}

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