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

标签 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>

这是我的接收者 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 中看不到任何消息。我如何发现我的下载未下载?

最佳答案

尝试添加一个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)

关于java - android.intent.action.DOWNLOAD_COMPLETE 不会在下载失败时收到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40103891/

相关文章:

java - 即使 JNDI 错误,Spring 上下文也会初始化

android - 使用.9.png工具制作渐变png

android - 在 Android 中按下按钮时增加按钮的大小

android - 未找到处理 Intent 的 Activity

android - Intent.action_pick_activity 的功能?

java - 如何连接midi接收器输出数据?

java - 读取 TXT 文件时 vector 越界

android - 试图访问 Assets 文件夹中的子文件夹

java - java页面上下文属性的垃圾收集

android - Droid-Fu 构建错误