Bopanna Bopanna - 3 years ago 100
Android Question

Android getIntent() returns the first intent

I have developed an application to download a video file and store it in the SD card. In the process I also update the progress and status of the download as a status bar notification using the


My class called the
extends the
. So here I update the progress using the
method where in I use the
for the purpose. Everything works like a charm except, on completion of download I want to click the notification to open the specific video file. So this is what i have done:

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

Note that the
are unique in my case.

opens the file by:

public void onCreate(Bundle 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/*");
} catch(Exception e) {

So when I download a video for the first time and click on the notification the appropriate video file will be opened. However next time when I download another video, and click on the notification the first file which was downloaded will be opened again.

This is because
returns the first
created and not the latest. How can I correct this?

Also, please note that the problem scenario exists when I download more than one video, e.g. if I download five different video files and there are five notifications in the status bar. The same file will be opened each time a notification is clicked.

Answer Source

@Alex and @Codinguser, thank you for your replies. Much appreciated. However I found a different answer that worked for me. When creating a PendingIntent for the Intent pass a unique value to it. In my case I was doing this:

mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0); 

but now I'm using NOTIFICATION_ID since it was unique. I've changed the above call to:

mContentIntent = PendingIntent.getActivity(mContext, NOTIFICATION_ID,
                                           notificationIntent, 0);

That's all and it works.

I found some information on it in the question Mulitple Instances of Pending Intent

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download