我开发了一个应用程序来下载视频文件并将其存储在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);
注意,fileName
和NOTIFICATION_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) {
//
}
}
所以当我下载视频的第一次,然后单击通知相应的视频文件将被打开:
的Activity
OpenDownloadedVideo.java
通过打开该文件。然而,下次我下载另一个视频时,点击通知,下载的第一个文件将再次打开。
这是因为getIntent
里面OpenDownloadedVideo
返回第一个Intent
创建而不是最新的。我该如何解决这个问题?
此外,请注意,当我下载多个视频时(例如,如果我下载了五个不同的视频文件,并且状态栏中有五个通知。每次点击通知时都会打开同一个文件。
怎么做呢?对不起,我对此很陌生。 – Bopanna
我不知道它是否有帮助。在清单编辑器中,您为活动启动模式添加SINGLE_TOP。然后你覆盖你的Activity的onNewIntent()并把相同的代码放在那里,就像你在onCreate中创建的 –
我做了'OpenDownloadedVideo'活动'SINGLE_TOP',并且在活动中我已经覆盖了'onNewIntent'以及你建议的.. ..但是当活动启动或重新启动时,它似乎并没有进入'onNewIntent'方法。 – Bopanna