11

RecognizerIntentに応答するアクティビティを実装しています。特に、このアクティビティは、保留中のインテントとそのエクストラバンドルを指定する2つの着信エクストラを処理する必要があります。

  • EXTRA_RESULTS_PENDINGINTENT
  • EXTRA_RESULTS_PENDINGINTENT_BUNDLE

ドキュメントの言い換え:

  • EXTRA_RESULTS_PENDINGINTENTを指定するために使用する場合PendingIntent、結果はバンドルに追加されPendingIntent、ターゲットに送信されます。

  • を使用EXTRA_RESULTS_PENDINGINTENTして転送インテントを提供する場合は、を使用EXTRA_RESULTS_PENDINGINTENT_BUNDLEして最終インテントに追加の追加を提供することもできます。検索結果がこのバンドルに追加され、結合されたバンドルがターゲットに送信されます。

私は次のことを示すサンプルコードを無駄に探していました。

PendingIntentバンドルからを抽出する最良の方法は何ですか?

私がすべきこと:

(PendingIntent)
        extras.getParcelable(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT)

の既存のエクストラのセットにエクストラを追加する方法はPendingIntent

変更を起動する方法はPendingIntent

4

3 に答える 3

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が作成されるときに、送信者が変更できるのはエクストラだけであることに注意してください。作成者はフラグを渡して他の部分を変更できるようにすることができますが、これは一般的には望ましくありません。

于 2011-07-01T00:57:11.033 に答える
1

私は自分のアプリで同様のことをしたので、2番目の質問について少し助けてあげることができるかもしれません。

インテントにエクストラを追加するのは、インテントで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
   }
}

これがいくつかの助けになることを願っています。

于 2011-06-27T19:28:32.047 に答える
1

These are my current answers to these questions. It works like this in a number of Google apps (Maps, Docs, YouTube, Listen) which all pass the PendingIntent to the RecognizerIntent when you perform the search via the microphone button. I am unsure though if this is the best (or most general) way of doing it. Any comments are welcome.

What is the best way of extracting a PendingIntent from a bundle?

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);

How to add extras to the set of existing extras of a PendingIntent?

Here we just create a new intent and put all the required extras into it.

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);

How to launch the modified PendingIntent?

We send off the PendingIntent giving it the new intent (with the new extras) as an argument.

try {           
    mExtraResultsPendingIntent.send(this, 1234, intent);
} catch (CanceledException e) {
    // Handle exception
}
于 2011-07-01T09:24:50.007 に答える