2011-04-12 27 views
2

我在寫媒體文件共享應用程序。意圖過濾器活動和正確顯示通知

現在被分成兩個部分:

<application> 
     <activity android:name="FileUploader" 
        android:label="..." 
        > 
      <intent-filter> 
       <action android:name="android.intent.action.SEND"/> 
       <data android:mimeType="*/*"/> 
       <category android:name="android.intent.category.DEFAULT"/> 
      </intent-filter> 
     </activity> 

    <service android:name=".FileUploaderService" /> 


    </application> 

FileUploader是應該從其他活動(例如廊或照相機。)推出的活性。 FileUploaderService是一個服務,它實際上傳文件到HTTP服務器。它執行多個HTTP請求。

現在有可能用戶在上傳過程中按Home按鈕並返回到主屏幕。 在這種情況下,我想通知當前上傳階段的詳細信息,當用戶點擊時會將用戶返回到活動狀態。

我creatung這樣的:

int icon = R.drawable.icon; 
    CharSequence tickerText = "Uploading File"; 
    long when = System.currentTimeMillis(); 

    Notification notification = new Notification(icon, tickerText, when); 

    Context context = getApplicationContext(); 
    CharSequence contentTitle = "Uploading File"; 
    CharSequence contentText = "Upload in progress. Stage #" + i; 

    Intent notificationIntent = new Intent(this, FileUploader.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      notificationIntent, 0); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.setLatestEventInfo(context, contentTitle, contentText, 
      contentIntent); 

    mNM.notify(R.layout.main, notification); 

的問題是,當我按在上傳過程中home鍵 - 這是從畫廊變得推出FileUploader活動實例「停止」,當我點擊通知 - 第二FileUploader活動實例已啓動。

那麼有什麼方法在點擊通知而不是啓動新通知時「返回」第一個活動?

我嘗試以下方法,但非他們在這裏工作:

  1. 調用完成()在FileUploader.onStop()方法 - 從縱向切換到橫向當活動被關閉。

  2. 設置android:launchMode =「singleTask」用於AndroidManifest.xml中的FileUploader活動 - 此後我的應用程序開始出現在Recent Applications列表中,這是不可接受的,因爲它允許從那裏啓動FileUploader活動,正如我所提到的在它上面只能通過從Gallery/Camera發送android.intent.action.SEND意圖來啓動。

  3. 用作用於發射第一個實例相同的目的 - 創造還是第二個實例和下面的警告是在日誌:

    04-12 17:01:17.041:警告/ ActivityManager(1289):startActivity從所謂非活動上下文;迫使Intent.FLAG_ACTIVITY_NEW_TASK爲:意向{ACT = android.intent.action.SEND典型值=圖像/ JPEG CMP = .FileUploader bnds = [0628] [480723](具有額外)}

我還試圖改變行:

Context context = getApplicationContext(); 

Context context = this; // FileUploadServiceContext 

但是,這並沒有幫助。

嗯。我想,也許我發動錯誤的活動。可能我應該爲我的FileUploader啓動「父」活動。 例如:如果我從Gallery啓動FileUploader,然後點擊Home,然後再次從主屏幕啓動Gallery - 我將返回到我的Activity。所以我想知道,也許我需要從通知中啓動Gallery/Camera。但有2個問題:

  1. 如何確定我的活動的父母究竟是什麼?

  2. 如果系統關閉了父母,該怎麼辦?

回答

0

好吧,我發現實現這個唯一的辦法是:

android:excludeFromRecents="true" android:launchMode="singleTask" 

這是我想要的(比如我不能回到「父」應用程序。)履歷表後不是100%,但總比沒有好。

1

您不能依賴於能夠在用戶離開導航後「返回」任何活動。操作系統可以並且可以很好地摧毀它,所以不會有任何回報。我建議您爲通知的意圖添加額外功能,並重新創建活動以在啓動時顯示來自這些額外功能的適當數據。

+0

我明白了。但在這種情況下,如何: 1.當我用主頁按鈕退出時終止第一個Activity實例,但在佈局更改時不會終止(例如)? 2.阻止我的活動顯示在最近的應用程序列表中? – Forsaken 2011-04-12 13:53:35

相關問題