Android getIntent()返回第一个意图

Android getIntent()返回第一个意图,android,android-intent,notifications,android-activity,Android,Android Intent,Notifications,Android Activity,我开发了一个应用程序来下载视频文件并将其存储在SD卡中。在此过程中,我还使用NotificationManager将下载的进度和状态更新为状态栏通知 我的类称为DownloadTask.java扩展了AsyncTask。因此,这里我使用onProgressUpdate()方法更新进度,其中我使用NotificationManager进行更新。一切都像一个魔咒,除了,在下载完成后,我想点击通知打开特定的视频文件。这就是我所做的: mNotificationManager = (Notificati

我开发了一个应用程序来下载视频文件并将其存储在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\u ID
在我的例子中是唯一的

活动通过以下方式打开文件:

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) {
        //
    }
}
因此,当我第一次下载视频并单击通知时,相应的视频文件将被打开。但是,下次当我下载另一个视频并单击通知时,下载的第一个文件将再次打开

这是因为
getIntent
内部
opendownloatedvideo
返回创建的第一个
Intent
而不是最新的。我怎样才能纠正这个问题


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

如果将您的
OpenDownloadedVideo
活动设置为单列,并从

请注意,getIntent()仍然返回原始意图。您可以使用setIntent(Intent)将其更新为新的Intent


因此,当您获得新的意图时,您需要显式地将其设置为活动意图。

@Alex和@Codinguser,感谢您的回复。非常感谢。然而,我找到了另一个对我有用的答案。为
目的创建
挂起内容时,向其传递唯一值。就我而言,我是这样做的:

mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0); 
但现在我使用的是
NOTIFICATION\u ID
,因为它是唯一的。我已将上述呼叫更改为:

mContentIntent = PendingIntent.getActivity(mContext, NOTIFICATION_ID,
                                           notificationIntent, 0);
仅此而已,它是有效的

我在问题中找到了一些关于它的信息

实际上,您只需使用PendingEvent.FLAG\u UPDATE\u CURRENT创建PendingEvent,如下所示:

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

在您想要启动的活动中。 添加(覆盖)以下功能:

@Override
protected void onNewIntent(final Intent intent) {
        super.onNewIntent(intent);
        this.setIntent(intent);
}

我该怎么做??对不起,我是个新手。我不知道这是否有用。在清单编辑器中,您可以为活动启动模式添加单_TOP。然后重写活动的onNewIntent(),并将与创建时相同的代码放入其中。我制作了
opendownloatedvideo
Activity
SINGLE\u TOP
,在活动中,我按照您的建议重写了
onNewIntent
。。。。但是当活动启动或重新启动时,它似乎没有进入
onNewIntent
方法中。@Bopanna可能它没有重新启动,而是暂停然后继续?此外,要检索新的意图,您必须覆盖onNewIntent(),调用getIntent()将返回旧的意图,即使您使用setIntent(),这是一个很好的答案。我基本上需要使用setIntent或调用onNewIntent()的super方法来确保getIntent返回最新的intent。
@Override
protected void onNewIntent(final Intent intent) {
        super.onNewIntent(intent);
        this.setIntent(intent);
}