Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 NotificationManager.

My class called the DownloadTask.java extends the AsyncTask. So here I update the progress using the onProgressUpdate() method where in I use the NotificationManager 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 fileName and NOTIFICATION_ID are unique in my case.

The Activity OpenDownloadedVideo.java opens the file by:

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 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 getIntent inside OpenDownloadedVideo returns the first Intent 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.

like image 451
Bopanna Avatar asked Aug 24 '12 10:08

Bopanna


People also ask

What is the use of getIntent in Android?

you can retrieve this data using getIntent in the new activity: Intent intent = getIntent(); intent. getExtra("someKey") ... So, it's not for handling returning data from an Activity, like onActivityResult, but it's for passing data to a new Activity.

What is getExtras ()?

getIntent().getExtras() is used to get values from intent that are stored in bundle. Intent class is used to switch between activities. But sometimes we need to send data from one activity to another.

What is intent in java?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Here is a sample example to start new activity with old activity.


1 Answers

From the Android Activity.onNewIntent() documentation

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

So when you get the new Intent, you need to explicitly set it as the activity intent.

like image 54
codinguser Avatar answered Oct 03 '22 21:10

codinguser