I developed an application to download a video file and save it to an SD card. In this process, I also update the progress and download status as a notification in the status bar using the NotificationManager .
My class, called DownloadTask.java , extends AsyncTask . So here I am updating the progress using the onProgressUpdate() method, where I use the NotificationManager for this purpose. Everything works like a charm, except that when the download is complete I want to click a notification to open a specific video file. So here is what I did:
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); int icon = android.R.drawable.stat_sys_download_done; long when = System.currentTimeMillis(); mNotification = new Notification(icon, "", when); mContentTitle_complete = mContext.getString(R.string.download_complete); notificationIntent = new Intent(mContext,OpenDownloadedVideo.class); notificationIntent.putExtra("fileName", file); mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0); mNotification.setLatestEventInfo(mContext, file, mContentTitle_complete, mContentIntent); mNotification.flags = Notification.FLAG_AUTO_CANCEL; mNotificationManager.notify(NOTIFICATION_ID, mNotification);
Please note that fileName and NOTIFICATION_ID unique in my case.
Activity OpenDownloadedVideo.java opens the file:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { fileName = getIntent().getExtras().getString("fileName"); Intent i = new Intent(Intent.ACTION_VIEW); File videoFileToPlay = new File(Environment.getExternalStorageDirectory()+"/MyFolder"+"/"+fileName); i.setDataAndType(Uri.fromFile(videoFileToPlay), "video/*"); startActivity(i); finish(); } catch(Exception e) {
So, when I download the video for the first time and click on the notification, the corresponding video file will open. However, the next time I upload another video and click on the notification, the first file that was downloaded will be opened again.
This is because getIntent inside OpenDownloadedVideo returns the first Intent created and not the last. How can i fix this?
Also note that a problem scenario exists when I upload multiple videos, for example. if I upload five different video files and there are five notifications in the status bar. The same file will be opened each time you click on a notification.