我正在使用 DownloadManager
类以编程方式下载文件。一切正常,但我无法让下载完成通知持续存在。下载完成后它会立即消失。这是我的代码:
Request rqtRequest = new Request(Uri.parse(((URI) vewView.getTag()).toString()));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
rqtRequest.setShowRunningNotification(true);
} else {
rqtRequest.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
((DownloadManager) getSystemService(DOWNLOAD_SERVICE)).enqueue(rqtRequest);
我在网上看到了一些与此相关的问题,但我找不到解决方案。
最佳答案
DownloadManager
不支持 Gingerbread 上的完成通知;你必须自己展示它。
使用 BroadcastReceiver to detect when the download finishes并显示您自己的通知:
public class DownloadBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
//Show a notification
}
}
}
并将其注册到您的 list 中:
<receiver android:name="com.zolmo.twentymm.receivers.DownloadBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
此外,setNotificationVisibility
是在 API 级别 11 (Honeycomb) 而不是 ICS 中添加的。我不确定您是否故意使用 ICS 常量,但您可以将代码更改为以下内容以在 Honeycomb 上也使用系统通知:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
rqtRequest.setShowRunningNotification(true);
} else {
rqtRequest.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
关于android - 为什么 "download completed"通知在 Gingerbread 设备上消失了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13349806/