我開發了一個應用程序來下載視頻文件並將其存儲在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