为什么“下载完成"?通知在姜饼设备上消失了吗? [英] Why does the "download completed" notification disappear on Gingerbread devices?

查看:83
本文介绍了为什么“下载完成"?通知在姜饼设备上消失了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DownloadManager类以编程方式下载文件.一切正常,但无法完成下载通知,无法继续.下载完成后,它将立即消失.这是我的代码:

I'm using the DownloadManager class to programatically download a file. All works fine but I can't get the download completed notifcation to persist. It disappears immediately once the download has completed. Here's my code:

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);

我已经在网上看到一些与此相关的问题,但找不到解决方案.

I've seen some questions around the web relating to this but I couldn't find a solution.

推荐答案

不支持有关Gingerbread的完成通知;您必须自己显示它.

DownloadManager doesn't support a completion notification on Gingerbread; you have to display it yourself.

使用 BroadcastReceiver来检测下载何时完成,然后显示您自己的通知:

Use a BroadcastReceiver to detect when the download finishes and show your own notification:

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
        }
    }
}

并将其注册在您的清单中

and register it in your manifest:

<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上的系统通知:

Also, setNotificationVisibility was added in API level 11 (Honeycomb) not ICS. I'm not sure if your use of the ICS constant is deliberate or not, but you can change your code to the following to the use the system notification on Honeycomb as well:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
    rqtRequest.setShowRunningNotification(true);  
} else {
    rqtRequest.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}

这篇关于为什么“下载完成"?通知在姜饼设备上消失了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆