Bop*_*nna 9 notifications android android-intent android-activity
我开发了一个应用程序来下载视频文件并将其存储在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);
Run Code Online (Sandbox Code Playgroud)
请注意,在我的情况下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) {
//
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当我第一次下载视频并单击通知时,将打开相应的视频文件.但是,下次当我下载另一个视频,然后单击该通知时,将再次打开下载的第一个文件.
这是因为getIntent内部OpenDownloadedVideo返回第一个Intent创建而不是最新.我怎么能纠正这个?
此外,请注意,当我下载多个视频时存在问题场景,例如,如果我下载五个不同的视频文件,并且状态栏中有五个通知.每次单击通知时都会打开相同的文件.
小智 11
实际上你只需要使用PendingIntent.FLAG_UPDATE_CURRENT创建PendingIntent,如下所示:
mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
来自Android Activity.onNewIntent()文档
请注意,getIntent()仍然返回原始Intent.您可以使用setIntent(Intent)将其更新为此新Intent.
因此,当您获得新的Intent时,您需要将其明确设置为活动意图.
/**
* 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.
}
Run Code Online (Sandbox Code Playgroud)
@Alex和@Codinguser,谢谢你的回复.非常感激.但是我找到了一个对我有用的不同答案.当创建一个PendingIntent用于Intent传递一个独特的价值吧.就我而言,我这样做:
mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
Run Code Online (Sandbox Code Playgroud)
但现在我正在使用,NOTIFICATION_ID因为它是独一无二的.我已将上述调用更改为:
mContentIntent = PendingIntent.getActivity(mContext, NOTIFICATION_ID,
notificationIntent, 0);
Run Code Online (Sandbox Code Playgroud)
这就是全部而且有效.
我在Mulitple Pending Intent中提到了一些关于它的信息
| 归档时间: |
|
| 查看次数: |
10360 次 |
| 最近记录: |