【问题标题】:android.intent.action.DOWNLOAD_COMPLETE don't recived on unsuccessful downloadandroid.intent.action.DOWNLOAD_COMPLETE 在下载失败时不接收
【发布时间】:2016-10-18 09:01:34
【问题描述】:

我有注册到 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 中看不到任何消息。我怎样才能发现我的下载没有下载?

【问题讨论】:

    标签: java android android-intent broadcastreceiver android-download-manager


    【解决方案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)

    【讨论】:

    • 问题是下载失败时根本没有调用onReceive
    • @Anton111111 您可以发布有关如何启动服务的代码吗?也许有什么我们可以帮你的
    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多