微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/android/317542.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐