我开发了一个应用程序,可以下载视频文件并将其存储在SD卡中。在此过程中,我还使用NotificationManager
更新下载进度和状态作为状态栏通知。
我的类名为DownloadTask.java
,它继承了AsyncTask
。所以,在这里,我使用onProgressUpdate()
方法更新进度,并使用NotificationManager
进行此操作。一切都很顺利,但是,在下载完成后,我希望单击通知以打开特定的视频文件。所以我做了以下工作:
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);
请注意,fileName
和NOTIFICATION_ID
在我的情况下是唯一的。
Activity
OpenDownloadedVideo.java
通过以下方式打开文件: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) {
//
}
}
当我第一次下载视频并点击通知时,将打开相应的视频文件。然而,下一次当我下载另一个视频并点击通知时,第一个下载的文件将再次被打开。
这是因为OpenDownloadedVideo
中的getIntent
返回的是第一个创建的Intent
而不是最新的Intent
。我该如何纠正这个问题呢?
此外,请注意,在我下载多个视频的情况下存在这个问题场景,例如如果我下载了五个不同的视频文件并且状态栏中有五个通知。每次点击通知时都会打开同一个文件。