2012-06-20 47 views
9

解决方案:需要API 11请参阅下面的答案!通知消失 - Android DownloadManager

简单问题:使用实施的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); 
     } 
     } 


}; 

}

+1

Plz发布了一些代码。 Thnx – CelticParser

+0

您还使用了哪些API? –

+0

@malger,你是否设法解决这个问题?我的通知也消失了。 –

回答

21

将正确的标记添加到您的请求中:

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

此下载是可见的,并显示在通知了正在进行和完成后。

+0

感谢您的帮助,但问题还没有解决!我得到: 'VISIBILITY_VISIBLE_NOTIFY_COMPLETED无法解析或不是字段 – malger

+0

您必须按照我所示的方式导入它。它链接在API – Blundell

+0

中,但我已经导入它:“ import android.app.DownloadManager; import android.app.DownloadManager.Request;” – malger