Android Wear Proview-通知から音声入力を受信する(Receiving Voice Input from a Notification)


------------------------------------------------------------------------------------
作者:Google
原文の住所:http://developer.android.com/wear/notifications/remote-input.html
译文著作権:Creative Commons 2.5 Attribution License
訳文者:[email protected]
本論文は2014-06-24バージョンに基づいて翻訳します.
訳文の著作権:CC BY-NC-ND 4.0,複製転載は許可されていますが、翻訳文の作者の署名と翻訳文のリンクを保留しなければなりません.演繹と商業用途に使用してはいけません.
------------------------------------------------------------------------------------
前言
もしあなたの通知にテキスト返信の操作が含まれているなら、例えばメールを返信するということは、通常ハンドヘルドデバイス上でActivityを起動して処理する必要があります.しかし、あなたの通知がAndroid Wearデバイスに表示されると、ユーザーは直接音声入力によって返信することができます.いくつかのプリセットされたテキスト情報をユーザーに提供してもいいです.
ユーザが音声を使用したり、予め設定された情報を選択して返信すると、システムはこの情報をあなたのハンドヘルドデバイス上のアプリケーションに送信します.この情報はあなたの通知操作に使用されるIntentのextra付加データとします.
注意:Androidシミュレータを使って開発する場合、音声応答のコントロールに文字を入力して返信してください.したがって、AVD設定では、Hardware keyboard presentの設定が開かれていることを確認します.
Android Wear Preview- 从通知上接收语音输入(Receiving Voice Input from a Notification)_第1张图片 Android Wear Preview- 从通知上接收语音输入(Receiving Voice Input from a Notification)_第2张图片
Define the Remote Input(リモート入力を定義)
音声入力をサポートする動作を作成するには、まずRemoteInput.BuiderのAPIを使用してRemoteInputオブジェクトのインスタンスを作成します.RemoteInput.Buiderのコンストラクタは文字列パラメータを必要とし、この文字列をハンドヘルドデバイス上のアプリケーションに転送するIntentが携帯するextraに付加された応答情報のkeyとする.
例えば、次のコードでRemoteInputオブジェクトを作成し、音声入力プロンプトにカスタムタグを提供します.
// Key for the string that's delivered in the action's intent
private static final String EXTRA_VOICE_REPLY = "extra_voice_reply";

String replyLabel = getResources().getString(R.string.reply_label);

RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
        .setLabel(replyLabel)
        .build();
Add Pr-defined Text Resonses(定義済みのテキスト回復を追加)
音声応答を許可する以外に、最大5つの予め定義された返信情報を提供してもいいです.set Choices()メソッドを呼び出して文字列配列を伝えるだけです.
例えば、いくつかの応答は、配列リソースファイルに定義できます.
res/values/strigs.xml


    
        Yes
        No
        Maybe
    
次に、文字列配列を取得し、RemoteInputオブジェクトに追加します.
String replyLabel = getResources().getString(R.string.reply_label);
String[] replyChoices = getResources().getStringArray(R.array.reply_choices);

RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
        .setLabel(replyLabel)
        .setChoices(replyChoices)
        .build();
Android Wear Preview- 从通知上接收语音输入(Receiving Voice Input from a Notification)_第3张图片
Receive Voice Input for the Primaryアクション(主な操作は音声入力を受信する)
「Reply」があなたの通知の主な操作(set Contintent Intent()によって定義されている場合は、addRemoteInput ForContintent()方法を使用してRemoteInputをメイン操作に追加することができます.たとえば:
// Create intent for reply action
Intent replyIntent = new Intent(this, ReplyActivity.class);
PendingIntent replyPendingIntent =
        PendingIntent.getActivity(this, 0, replyIntent, 0);

// Build the notification
NotificationCompat.Builder replyNotificationBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_new_message)
        .setContentTitle("Message from Travis")
        .setContentText("I love key lime pie!")
        .setContentIntent(replyPendingIntent);

// Create the remote input
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
        .setLabel(replyLabel)
        .build();

// Create wearable notification and add remote input
Notification replyNotification =
        new WearableNotifications.Builder(replyNotificationBuilder)
        .addRemoteInputForContentIntent(remoteInput)
        .build();
通知された主操作には、addRemoteInput ForContintentIntent()方法を用いてRemoteInputオブジェクトを追加することにより、このボタンは通常、「Open」ボタンとして表示され、ユーザーがAndroid Wearデバイス上で選択してから「Reply」ボタンに遷移し、音声入力画面の表示を開始することが分かる.
Receive Voice Input for a Secondaryアクション(二次操作のために音声入力を受信する)
もし「Reply」操作があなたの通知主操作ではなく、音声入力を二次操作としたいです.アクションオブジェクトを使って新しい操作ボタンを定義して、RemoteInputオブジェクトを送ることができます.
アクション・ビルダーを使ってアクションオブジェクトを具体的に例証するべきです.この構造方法はアイコンとテキストラベルが必要です.また、オブジェクトをパラメータとして設定するためにPendingIntentが必要です.この中で、ユーザがこのアクションを選択すると、システムはこのPendingIntentを通じてあなたのアプリケーションを呼び出します.たとえば:
// Create the pending intent to fire when the user selects the action
Intent replyIntent = new Intent(this, ReplyActivity.class);
PendingIntent pendingReplyIntent =
        PendingIntent.getActivity(this, 0, replyIntent, 0);

// Create the remote input
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
        .setLabel(replyLabel)
        .build();

// Create the notification action
Action replyAction = new Action.Builder(R.drawable.ic_message,
        "Reply", pendingIntent)
        .addRemoteInput(remoteInput)
        .build();
RemoteInputオブジェクトをアクションに追加すると、このアクションをWearable Notifications.Builderに追加する必要があります.たとえば:
// Create basic notification builder
NotificationCompat.Builder replyNotificationBuilder =
        new NotificationCompat.Builder(this)
        .setContentTitle("New message");

// Create the notification action and add remote input
Action replyAction = new Action.Builder(R.drawable.ic_message,
        "Reply", pendingIntent)
        .addRemoteInput(remoteInput)
        .build();

// Create wearable notification and add action
Notification replyNotification =
        new WearableNotifications.Builder(replyNotificationBuilder)
        .addAction(replyAction)
        .build();
現在、ユーザーがAndroid Wearデバイス上で「Reply」を選択した後、システムはユーザーの音声入力を提供しています.ユーザが返信応答を完了すると、システムは操作に付加されたIntentを呼び出し、EXTRA_を追加します.VOICE_REPLY(つまり、RemoteInput.Buiderコンストラクタで伝達される文字列)はkeyであり、ユーザーが情報を入力する文字列の値であるextraをIntentに入力する.