我有注册到 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/