Android getIntent () returns first intention

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.

+6
source share
5 answers

@Alex and @Codinguser, thanks for your answers. Very grateful. However, I found another answer that worked for me. When creating a PendingIntent for an Intent pass it a unique value. In my case, I did this:

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

but now i am using NOTIFICATION_ID as it is unique. I changed the call above:

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

That everything works.

I found some information about this in the Mulitple Instances of Pending Intent question

+4
source

in fact you just need to create a PendingIntent with PendingIntent.FLAG_UPDATE_CURRENT , for example:

 mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
+10
source

From Android Documentation Activity.onNewIntent ()

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

So, when you get a new intention, you need to explicitly indicate it as the intention of the activity.

+6
source
 /** * Override super.onNewIntent() so that calls to getIntent() will return the * latest intent that was used to start this Activity rather than the first * intent. */ @Override public void onNewIntent(Intent intent){ super.onNewIntent(intent); // Propagate. setIntent(intent); // Passing the new intent to setIntent() means this new intent will be the one returned whenever getIntent() is called. } 
+4
source

What if you do your OpenDownloadedVideo SINGLE_TOP activity and override onNewIntent ?

0
source

Source: https://habr.com/ru/post/923654/


All Articles