Android getIntent()返回第一个intent

我开发了一个应用程序来下载视频文件并将其存储在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,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.我怎么能纠正这个?

此外,请注意,当我下载多个视频时存在问题场景,例如:如果我下载五个不同的视频文件,状态栏中有五个通知.每次单击通知时都会打开相同的文件.

解决方法

实际上你只需要使用PendingIntent.FLAG_UPDATE_CURRENT创建PendingIntent,如下所示:
mContentIntent = PendingIntent.getActivity(mContext,PendingIntent.FLAG_UPDATE_CURRENT);

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...