2012-08-24 24 views
6

我开发了一个应用程序来下载视频文件并将其存储在SD卡中。在这个过程中,我还使用NotificationManager作为状态栏通知来更新下载的进度和状态。Android getIntent()返回第一个意图

我的课程名为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); 

注意,fileNameNOTIFICATION_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) { 
     // 
    } 
} 

所以当我下载视频的第一次,然后单击通知相应的视频文件将被打开:

ActivityOpenDownloadedVideo.java通过打开该文件。然而,下次我下载另一个视频时,点击通知,下载的第一个文件将再次打开。

这是因为getIntent里面OpenDownloadedVideo返回第一个Intent创建而不是最新的。我该如何解决这个问题?

此外,请注意,当我下载多个视频时(例如,如果我下载了五个不同的视频文件,并且状态栏中有五个通知。每次点击通知时都会打开同一个文件。

回答

4

@Alex和@Codinguser,谢谢你的回复。非常感激。不过,我发现了一个适合我的不同答案。当为Intent创建PendingIntent时,会为其传递一个唯一值。在我的情况下,我这样做:

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

但现在我使用NOTIFICATION_ID,因为它是独特的。我已将上述呼叫更改为:

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

就是这样,它有效。

我发现了一些关于它的信息的问题 Mulitple Instances of Pending Intent

0

如果您的OpenDownloadedVideo活动SINGLE_TOP和覆盖onNewIntent该怎么办?

+0

怎么做呢?对不起,我对此很陌生。 – Bopanna

+0

我不知道它是否有帮助。在清单编辑器中,您为活动启动模式添加SINGLE_TOP。然后你覆盖你的Activity的onNewIntent()并把相同的代码放在那里,就像你在onCreate中创建的 –

+0

我做了'OpenDownloadedVideo'活动'SINGLE_TOP',并且在活动中我已经覆盖了'onNewIntent'以及你建议的.. ..但是当活动启动或重新启动时,它似乎并没有进入'onNewIntent'方法。 – Bopanna

6

Android Activity.onNewIntent() documentation

注意getIntent()仍返回原来的意图。你可以使用setIntent(Intent)将它更新到这个新的Intent。

因此,当您获得新的Intent时,您需要明确将其设置为活动意图。

+0

另外,为了检索你必须覆盖onNewIntent()的新的意图,调用getIntent()将返回旧的,即使你使用setIntent() – DoruChidean

+0

这是一个很好的答案。我需要基本上使用setIntent或调用onNewIntent()的super方法来确保getIntent返回最新的意图。 – welshk91

10

其实你只需要的PendingIntent创建的PendingIntent。FLAG_UPDATE_CURRENT,像这样:

mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
4
/** 
* 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. 
}