有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓。意图行动下载完成-下载不成功时未收到

我已经收到了注册到安卓的邮件。意图行动下载完整

AndroidManifest。xml

       <uses-permission 安卓:name="安卓.permission.INTERNET" />
       <receiver
            安卓:name="com.myapp.listener.DownloadListenerService"
            安卓:exported="true"
            安卓:enabled="true">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.DOWNLOAD_COMPLETE" />
            </intent-filter>
        </receiver>

这是我的接收器下载服务。爪哇:

package com.myapp.listener;

import 安卓.content.BroadcastReceiver;
import 安卓.content.Context;
import 安卓.content.Intent;

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

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


共 (1) 个答案

  1. # 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;
    }
    

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