Android Wearで通知から音声入力を受け取る

Get Started(導入)とCreating Notifications for Android Wear(Wear用の通知を作成する)は次の記事を参照して下さい。
GoogleがスマートウォッチOS “Android Wear” を発表したのでテスターになってみた
Android Wearで通知から音声入力を受け取る
 
先日に引き続き、Android Wearを動かして見たいと思います。

通知から音声入力を受け取る

前回同様、今日は次のページを翻訳しながら読み進めていきたいと思います。
Receiving Voice Input from a Notification

もし、あなたの通知に、電子メールに返信するなどといったテキストで応答するアクションがあれば、通常は携帯端末でアクティビティを起動する必要があります。しなしながら、通知がAndroidウェアラブルに表示されているとき、あなたは音声入力で直接返信することが出来ます。また、ユーザがー選択できるテキストメッセージ事前に用意して提供することも出来ます。
ユーザーが音声または表示されたメッセージを選択するかして返信するとき、システムは接続されている携帯デバイスのアプリケーションにメッセージを送信します。メッセージには、あなたが通知アクションに使用する指定したインテントの追加情報が、追加されています。
注意:エミュレータで開発する場合、AVDの設定でHardware keyboard presentを有効にして、音声入力フィールドでテキストを入力し応答する必要があります。

RemoteInputを定義する

音声入力をサポートするアクションを作成するには、まずRemoteInput.Builder APIを使用してRemoteInputのインスタンスを作成します。RemoteInput.Builder コンストラクタはシステムがハンドヘルド上のアプリへの返信メッセージを運ぶためのインテントのExtraのキーとして使用する文字列を受け取ります。
例えば、音声入力のカスタムラベルを提供する新しいRemoteInputオブジェクトを作るには次のようにします。
[java]
// 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();
[/java]

定義済みテキストによる返答を追加

音声入力を可能にすることに加え、ユーザーが素早く返信出来るように5つのテキストを提供することが出来ます。setChoices()を呼び出し、文字列配列を渡します。
例えば、リソース配列にいくつかの応答を追加することができます。
 
 
res/values/strings.xml
[xml]
<?xml version=”1.0″ encoding=”utf-8“?>
<resources>
<string-array name=”reply_choices”>
<item>Yes</item>
<item>No</item>
<item>Maybe</item>
</string-array>
</resources>
[/xml]
その後、インフレートして文字列配列を作成しRemoteInputに追加します。

[java]
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();
[/java]

プライマリアクションとして音声入力を受け取る

もし、「返信が」アプリケーションのプライマリアクション(setContentIntent()で定義された)ならば,addRemoteInputForContentIntent()を使ってメインアクションにRemoteInputをアタッチしなければなりません。例えば
[java]
// 返信アクションのインテントを作成
Intent replyIntent = new Intent(this, ReplyActivity.class);
PendingIntent replyPendingIntent =
PendingIntent.getActivity(this, 0, replyIntent, 0);
// 通知の作成
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);
// リモート入力の作成
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(replyLabel)
.build();
// ウェアラブル通知の作成とリモート入力の追加
Notification replyNotification =
new WearableNotifications.Builder(replyNotificationBuilder)
.addRemoteInputForContentIntent(remoteInput)
.build();
[/java]
addRemoteInputForContentIntent()を使ってRemoteInputオブジェクトを通知のプライマリアクションとして追加したことで、Android Wearでそれを選択した時、ボタンは通常Openと表示されていたアクションがReplyアクションと音声入力を開始するUIになります。

セカンダリアクションとして音声入力を受け取る

返信アクションが通知のプライマリアクションではない場合や音声入力をセカンダリアクションとして有効にしたい場合、Actionオブジェクトとして定義した新しいアクションボタンとしてRemoteInputを追加します。
アイコンとアクションボタンへのテキストラベルのどちらかを取り、さらに、PendingIntent、ユーザがアクションを選択した時にシステムがアプリを呼び出せるようにペンディングインテントを、Action.Builder()コンストラクタでActionのインスタンスを作成してください。例えば:
[java]
// ユーザがアクションを選択した時に呼ばれるペンディングインテントを作成
Intent replyIntent = new Intent(this, ReplyActivity.class);
PendingIntent pendingReplyIntent =
PendingIntent.getActivity(this, 0, replyIntent, 0);
// RemoteInputを作成
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(replyLabel)
.build();
// 通知Actionを作成
Action replyAction = new Action.Builder(R.drawable.ic_message,
"Reply", pendingIntent)
.addRemoteInput(remoteInput)
.build();
[/java]
ActionにRemoteInputを追加した後で、addAction()を用いてWearableNotifications.Builderにアクションを追加します。例えば:
[java]
// 通常の通知ビルダーを作成します
NotificationCompat.Builder replyNotificationBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("New message");
// 通知アクションを作成しRemoteInputを追加します
Action replyAction = new Action.Builder(R.drawable.ic_message,
"Reply", pendingIntent)
.addRemoteInput(remoteInput)
.build();
// WearableNotificationsを作成しアクションを追加します
Notification replyNotification =
new WearableNotifications.Builder(replyNotificationBuilder)
.addAction(replyAction)
.build();
[/java]
ユーザがAndroidウェアラブルで「返信」を選択した時、システムは音声入力をプロンプトに表示する(あと事前定義返信のリストを表示する。提供していれば)。ユーザーが応答を完了すると、システムはアクションに付加されたインテントにユーザのメッセージ文字列をEXTRA_VOICE_REPLY(RemotInputBuilderコンストラクタに渡した文字列)として追加して起動しする。

上部へスクロール