2011-06-24 20 views
10

我正在實施一個響應RecognizerIntent的活動。等等這一活動必須處理兩個來電額外指定一個懸而未決的意圖和演員束:RecognizerIntent:如何將一個包添加到待處理的意圖

  • EXTRA_RESULTS_PENDINGINTENT
  • EXTRA_RESULTS_PENDINGINTENT_BUNDLE

複述的文檔:

  • 如果您使用EXTRA_RESULTS_PENDINGINTENT來提供PendingIntent,則結果lts將被添加到它的捆綁包中,並且PendingIntent將被髮送到它的目標。

  • 如果您使用EXTRA_RESULTS_PENDINGINTENT來提供轉發意向,您還可以使用EXTRA_RESULTS_PENDINGINTENT_BUNDLE爲最終意向提供額外的附加功能。搜索結果將被添加到該捆綁包中,並且組合捆綁包將被髮送到目標。

我一直在尋找示範以下代碼示例代碼。

從包中提取PendingIntent的最佳方法是什麼?

我應該做的:

(PendingIntent) 
     extras.getParcelable(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT) 

如何額外添加到集PendingIntent現有的臨時演員?

如何啓動修改後的PendingIntent

+0

請檢查下面的鏈接,它是searchDialog.java的來源,你可以深入研究,你可以清楚你的答案。 http://www.devdaily.com/java/jwarehouse/android/core/java/android/app/SearchDialog.java.shtml – Nikhil

回答

1

這些是我目前對這些問題的回答。它在許多Google應用程序(地圖,文檔,YouTube,聽)中都是這樣工作的,當您通過麥克風按鈕執行搜索時,所有這些應用程序都將PendingIntent傳遞到RecognizerIntent。我不確定這是否是最好的(或最一般的)做法。歡迎任何評論。

從包中提取PendingIntent的最佳方法是什麼?

Parcelable extraResultsPendingIntentAsParceable = 
      bundle.getParcelable(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT); 
if (extraResultsPendingIntentAsParceable != null) { 
    if (extraResultsPendingIntentAsParceable instanceof PendingIntent) { 
     mExtraResultsPendingIntent = 
         (PendingIntent) extraResultsPendingIntentAsParceable; 
    } else { 
     // Report an error 
    } 
} 

mExtraResultsPendingIntentBundle = 
      bundle.getBundle(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE); 

如何額外添加到集PendingIntent現有的臨時演員?

在這裏,我們只是創建一個新的意圖,並把所有必要的額外內容。

if (mExtraResultsPendingIntentBundle == null) { 
    mExtraResultsPendingIntentBundle = new Bundle(); 
}    
Intent intent = new Intent(); 
intent.putExtras(mExtraResultsPendingIntentBundle); 
// Unsure about the following line... 
// Should I use another name for the extra data (instead of SearchManager.QUERY) 
intent.putExtra(SearchManager.QUERY, speechRecognitionResult); 

如何啓動修改PendingIntent

我們發送PendingIntent給它一個新的意圖(以新的附加)作爲參數。

try {   
    mExtraResultsPendingIntent.send(this, 1234, intent); 
} catch (CanceledException e) { 
    // Handle exception 
} 
+0

捆綁包從哪裏來? –

+1

@IgorG。我不確定我是否理解......上述代碼的所有輸入都來自額外內容。 – Kaarel

+0

只是想確保你的bundle變量是我所設想的... –

1

因爲我在自己的應用程序中做過類似的事情,所以我可能會爲您提供第二個問題的幫助。

向意圖添加附加內容應該像在意圖上調用putExtra()一樣簡單。我已經完成了通知。

Intent notificationIntent = new Intent(_context, MyActivity.class); notificationIntent.putExtra("SOME_ID", "VALUE");

這是啓動我的應用程序通知的一部分。我後來在我的活動恢復時閱讀了額外的內容。

Intent intent = getIntent(); 
Bundle extras = intent.getExtras(); 
if(extras !=null) 
{ 
    String value = extras.getString("SOME_ID"); 
    if(value != null && value.equals("VALUE")) 
    { 
     //Value was found, do something 
    } 
} 

希望這有助於一些。

+2

謝謝,但PendingIntent與Intent不太一樣。 – Kaarel

4

出於安全原因,您無法直接觸摸PendingIntent的內容。但是,當您發送PendingIntent時,您有機會根據原創者允許的內容對其內容進行補充或修改。

這是您要使用發送的PendingIntent方法:

http://developer.android.com/reference/android/app/PendingIntent.html#send(android.content.Context, int, android.content.Intent, android.app.PendingIntent.OnFinished, android.os.Handler)

您提供此處的目的是用來修改從的PendingIntent發送的最終意圖的數據。

什麼可以修改的規則在這裏:

http://developer.android.com/reference/android/content/Intent.html#fillIn(android.content.Intent, int)

注意,由時的PendingIntent創建默認情況下,僅可以通過發送修改的部分是臨時演員。創建者可以通過標誌來允許修改其他部分,儘管這通常是不期望的。

+0

謝謝,這是我以後的事。 – Kaarel

+1

不客氣。我應該澄清一點 - 填寫意圖的默認規則是隻能修改附加內容,但如果其他字段尚未包含數據,則可以設置其他字段。例如,默認情況下,如果已設置,則無法更改操作。 PendingIntent的創建者可以使用這些標誌來表示允許發件人修改該字段......但當然這會使他們面臨重大的安全問題,因爲這意味着他們給予PendingIntent的人可以修改它以執行非常不同的事情。 – hackbod

+0

如果我正在使用PendingIntent.getBroadcast獲取PI,那麼我需要什麼特殊步驟來強制它保留現有的Intent extras PI最初是通過何種方式創建的?我正在嘗試上述方法,但額外功能總是無效。 – Michael

相關問題