15

解决方案:需要 API 11,请参阅下面的答案!

简单问题:使用实施的 DownloadManager 下载文件后,通知消失。下载后如何强制保留通知?

我尝试使用 VISIBILITY_VISIBLE_NOTIFY_COMPLETED,但我不知道如何使用它

感谢您为解决此问题提供的任何帮助;)

编辑:代码

public class BgDL extends Activity {

private DownloadManager mgr = null;
private long id;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    mgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

    Request request = new Request(Uri.parse(getIntent().getStringExtra("URL")));

    id = mgr.enqueue(request
            .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "UPDATE")
            .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false)
            .setTitle("APP update")
            .setDescription("New version "+getIntent().getDoubleExtra("OV", 0.0))


    );

   registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

}
BroadcastReceiver receiver = new BroadcastReceiver () {


      public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(mgr.ACTION_DOWNLOAD_COMPLETE) ){
            unregisterReceiver(receiver);
            finishActivity(99);
        }
      }


}; 

}

4

1 回答 1

33

在您的请求中添加正确的标志:

Request request = new Request(Uri.parse(getIntent().getStringExtra("URL")));

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

参考:

http://developer.android.com/reference/android/app/DownloadManager.Request.html#setNotificationVisibility(int)

控制下载管理器是否在下载运行或下载完成时发布系统通知。如果启用,下载管理器会通过系统 NotificationManager 发布有关下载的通知。默认情况下,仅当下载正在进行时才会显示通知。

http://developer.android.com/reference/android/app/DownloadManager.Request.html#VISIBILITY_VISIBLE_NOTIFY_COMPLETED

此下载在进行中和完成后可见并显示在通知中。

于 2012-06-20T21:27:55.647 回答