2012-08-24 20 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. 
}