15

Solution: API 11 is needed see answer below!

Easy Question: After downloading a File with the implemented DownloadManager the Notification disappears. How do I force the Notification to stay after Download?

I tried to use VISIBILITY_VISIBLE_NOTIFY_COMPLETED, but i do not know how i can use it

Thank for any kind of help to solve this problem ;)

EDIT: Code

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


}; 

}

malger
  • 173
  • 1
  • 2
  • 7

1 Answers1

33

Add the correct flag to your request:

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

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

Reference:

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

Control whether a system notification is posted by the download manager while this download is running or when it is completed. If enabled, the download manager posts notifications about downloads through the system NotificationManager. By default, a notification is shown only when the download is in progress.

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

This download is visible and shows in the notifications while in progress and after completion.

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • thank you for help, but problem isn't solved yet! I get: 'VISIBILITY_VISIBLE_NOTIFY_COMPLETED cannot be resolved or is not a field – malger Jun 20 '12 at 21:35
  • but i have imported it: " import android.app.DownloadManager; import android.app.DownloadManager.Request;" – malger Jun 20 '12 at 21:45
  • 2
    it needs a minSdk of 11 you have probably got 9? (in your AndroidManifest) API url I linked: "`Since: API Level 11`" – Blundell Jun 20 '12 at 21:46
  • 1
    ohh..... mh that seems to be the problem. but what can I do to get this work under gingerbread API 9? Any kind of solution? – malger Jun 20 '12 at 21:49
  • I guess i have to use NotificationManager instead? – malger Jun 20 '12 at 21:54
  • 1
    Nopes, i think you guys are wrong. On earlier devices, you have to use the `Request.setShowRunningNotification(true);` to show the download notification. It's right there in the docs. http://developer.android.com/reference/android/app/DownloadManager.Request.html#setShowRunningNotification%28boolean%29 – Mridang Agarwalla Nov 12 '12 at 18:27