解决方案:需要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);
}
}
};
}
答案 0 :(得分:24)
在您的请求中添加正确的标记:
Request request = new Request(Uri.parse(getIntent().getStringExtra("URL")));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
参考:
控制下载管理器在下载运行或完成下载时是否发布系统通知。如果启用,下载管理器将通过系统NotificationManager发布有关下载的通知。默认情况下,仅在下载正在进行时才会显示通知。
此下载可见,并在进行中和完成后显示在通知中。