GoogleがスマートウォッチOS “Android Wear” を発表したのでテスターになってみた

GoogleがスマートウォッチOSを発表しましたね。

Google、スマートウォッチOS、Android Wearを発表―最初の製品はLG ‘G’とMotorola Moto 360

 

http://jp.techcrunch.com/2014/03/19/20140318googles-android-wear-a-nexus-program-for-smartwatches-debuts-with-the-lg-g/

それでは早速、Google開発者ブログより、開発者プレビューに登録して、一足先にAndroid Wearableを体験しましょう。
開発者プレビューに登録すると24時間以内に返信が来るようです。さて返信は来るでしょうか?...しばらく待つと30分ほどでnoreply@google.comよりメールがきました。それでは、メールの内容を見ながら作業を進めます。
Android Wareの開発を始めるにはPreview Support libraryとスマフォ用Android Ware Previewアプリが必要なようです。
まずはPreview Support ライブラリとサンプルをサウンロードします。送られてきたメールのリンクをクリックするとAndroidWearPreview.zipがダウンロードされます。
つぎに、Google Play StoreでAndroid Wear Previewテスターとなるためにオプトインします。
メールのリンクをクリックするとAndroid Wareのテスターになるための招待画面が表示されるので「テスターになる」をクリックします。

 
「Play ストアから「Android Wear Preview」をダウンロード」をクリックしてみると、「このアプリはお使いのどの端末にも対応していません。」と表示されてしまいます。う〜ん、どうすればいいんだろう?私の持っているAndroid端末には対応してないのか?
どうもKitKatの最新が入っている端末出ないとダメなようですね。私のAndorid携帯であるXperia Z(SO-02E)は、Android 4.2.2 Jelly Beanなのでインストール出来ません。ううむ。困った。
ではNexus7(1st gen)ではどうだ?Android4.2.2にアップデート済みです。

 
残念ながらこちらも「お使いの端末はこのバージョンに対応していません。」となってしまいました。g+のスレッドではNexus7(2013)でも同様の状況であるようです。
この辺は改善されていくでしょうが、このまま指をくわえて待っているのも嫌。かといってNexus5を購入するってのもちょっと高いしなあ。
ということで、サイドロードしてみましょう。こちらより入手します。
https://drive.google.com/file/d/0B4d9d7-eZ4P0QUFPSlZEOVlMeVdSR1RfdzkwWFNiN0NwVGkw/edit?usp=sharing
 
com.google.android.wearablepreview.app-1.0.0.1077241.apk というファイルをダウンロード、端末に転送してインストールします。

 
よし、うまく行きました。これで進められそうです。
近々、Nexus7などの非携帯電話端末でも使えるようになるようです。
 
さて次にAndroid Wearについての情報を得ます。詳細はAndroid Wear Developer Get Started ページを参照して下さいとの事。
注意事項として、「これはプレビューリリースであるため、公にアプリケーションを配布しないで下さい。」とあります。
またAPIはリリースされた際には変更されてアプリケーションを修正しないといけない場合もある点も注意が必要です。
Android Ware 開発者コミュニティに参加してあなたの得た経験を共有したり質問したりすることが出来ます。英語ですが有益な情報がやり取りしておくのでウォッチしておくと良いです。
さて、では Get Started ページを見ながら、セットアップをしていきましょう。
1.Install the Android SDK.
Android SDKにはAndroidのためのアプリケーションを構築するために必要なすべての開発ツールが含まれています(オプションのIDEもダウンロードできます)。 さくっとダウンロードしてインストールしてしまいましょう。
2.Sign up for the Android Wear Developer Preview.
ADTプラグインのバージョンはversion 22.6.1以上が必要です。既にAndroidの開発環境を持っている場合は確認が必要でしょう。
次にAndroidWare システムイメージのインストールです
1.Android SDKマネージャを起動
2.22.6以上に更新
Android SDK Toolsを22.6以上に更新します。

 

1.Android SDK Toolsを選択

2.Install pakageをクリック

3.ライセンスを承認しinstallをクリック

4.インストールが完了したらAndroid SDK Managerを再起動します。

3.Android Wear ARM EABI v7a System Imageを選択します。

4.Android Support Libraryを選択します。
5.Install packagesをクリックします
6.ライセンスを承認しInstallをクリックします。
ここで問題が発生。
インストール後、Eclipseを再起動すると
This Android SDK requires Android Developer Toolkit version 22.6.1 or above.
Current version is 22.3.0.v201310242005-887826.
Please update ADT to the latest version.
というエラーが表示されてしまいました。

 
その場合は次の手順で解決できます。

メニューのHepl – Install new software…を選択します。

Work withで「Add」をクリックして次のURLを入力します。

https://dl-ssl.google.com/android/eclipse/

「Developer Tools」にチェックを入れ「Next>」をクリック

 

「Finish」をクリックします。

 
 

Securty Warningが表示されたらOKで閉じます。

 

インストールが完了するとEclipseを再起動するか尋ねられます。「Yes」を選択してEclipseを再起動します。

 
よし、これで大丈夫。
 
それでは、次にAndroid Wear Emulatorを設定します
1.Android Virtual Device Managerを起動します
2.Newをクリックします
3.AVD Nameに四角形ディスプレイなら”AndroidWearSquare”または丸型ディスプレイなら”AndroidWearRound”と入力します。
4.DeviceでAndroid Wear Square(四角形) もしくは Android Wear Round(丸型)を選択します。
5.Targetに「Android 4.4.2 – API Level 19」を選択します。
6.CPU/ABIに「Android Wear ARM (armeabi-v7a)」を選択します。
7.SkinででAndroid Wear Square(四角形) もしくは Android Wear Round(丸型)を選択します。
8.その他のオプションはデフォルトのまま、「OK」をクリックします。実際のAndroid Wareデバイスには入力方式としてキーボードがないのですが、音声入力のかわりに画面上でテキスト入力出来るように「Hardware keyboard present」にチェックしておく必要があります。

9.AVDのリストで作成したデバイスを選択して「Start」をクリックします。Windowが開くので「Launch」をクリックします。
Android Wearエミュレータが起動します。アプリの通知テストを開始するには、Android Wear Preview アプリをインストールした開発用デバイスが必要です。
エミュレータの起動時間を改善するにはAVDの設定でEmulator Optionの下のSnapshotを有効にします。エミュレータを起動したら、Save to shapshotを選択してから起動します。エミュレータが起動すると、終了時にスナップショットが保存されます。再度AVDを起動した時、Launch from snapshotを選択状態にして、Save to snapshotの選択は解除しておきます。
注意:Android Wear amulatorにアプリをインストールしないで下さい。今までのAndroidアプリはサポートしておらず、そのようなアプリを実行した結果は予測出来ません。
3.Android Wear Previewアプリのセットアップ
アプリの通知をAndroid Wear エミュレータに配信するため、Android Wear PreviewアプリをAndroidにインストールする必要があります。
Android Wear Previewアプリをインストールするには、Google Play StoreでDeveloper Previewにサインアップする必要があります。
注意:Android Wear Previewアプリは、Android4.3以上で使用し、Androidエミュレータでは使用できません。
Android Wear Previewアプリは先ほどインストールしましたのでOK。先へ進みます。
Android Wear PreviewアプリをインストールしたらAndroid Wearエミュレータと通信するように設定します。
1.Android Wear Previewアプリを起動します。現在アプリは通知リスナーとして有効になっていないという通知が表示されていると思います。メッセージをタップしてシステム設定を開き、Android Wear PreviewをタップしてAndroid Wear Previewを有効にします。

 
Android Wear Previewをタップします。

 
Android Wear Previewを有効にしますか?」ダイアログで「OK」をタップします。

 
Android Wear Previewにチェックが入り、通知が有効になりました。

2.デバイスを開発マシンにUSB接続します。他のデバイスが開発マシンに接続されていない事を確認します。
3.前のセクションで作成したAndroid Wear エミュレータが起動している事を確認します。エミュレータは時刻と、デバイスが接続していない事を示すアイコンが表示されています。

コマンドターミナルを開きAndroid SDKの “platform-tools/”ディレクトリに移動して次のコマンドを実行します。
[shell light=””true””]
adb -d forward tcp:5601 tcp:5601
[/shell]
 
私の環境(Mac)では以下のようにコマンドを実行しました。
[shell light=””true””]
cd /Applications/adt-bundle-macx86_64-20131030/sdk/platform-tools
./adb -d forward tcp:5601 tcp:5601
[/shell]
5.Android Wear Previewアプリに戻ると、エミュレータに接続されていると思います。
これでデバイスからの通知をAndroid Wearエミュレータに表示できるようになりました。

例えば、端末にメールが届くと、Android Wearエミュレータに表示されます。

 
4.サポートライブラリをプロジェクトに追加する
Android Wear preview support libraryには、Android Wearのユーザー体験のためにアプリの通知を最適化することが出来るようにするため、いくつかのAPIが含まれています。
ライブラリを入手するには、Developer Previewに参加したときに受け取ったメールの中に、サポートライブラリといくつかのサンプルアプリが入ったZIPファイルへのダウンロードリンクがあるので、リンクをクリックしてダウンロードします。
ダウンロードしたZIPファイルを展開し、サポートライブラリをプロジェクトに追加します。
まずは適当にプロジェクトを作成しましょう。詳細は下記画像をクリックして参照してください。

 
1.Androidアプリのプロジェクトのルートに libs/ ディレクトリを作成します。通常は既に出来ていると思います。

2.v4 サポートライブラリのJARファイルを libs/ ディレクトリに配置します。これも既に追加されています。
3.同じくwearable-preview-support.jar ファイルを libs/ ディレクトリに配置します。ドラッグ&ドロップでコピーできます。

 

 
4.JARファイルを右クリックしBuild Path > Add to Build Path を選択してビルドパスに追加します。

これで開発の準備は整いました。「通知を最適化するためにはCreating Notifications for Android Wearを読んで下さい。」という事ですので、読み進めていきましょう。
 
 
 
Android Wearへの通知を作成する
スマートフォンタブレットといったAndroidバイスAndroid Wearに接続されている場合、デフォルトではすべての通知がデバイス間で共有されます。Android Wearではそれら通知が新しいカードとしてコンテキストストリームに表示されます。
だからなにも努力をしないでも、アプリの通知はAndroid Wearの利用者が利用できます。とはいえ、幾つかの方法でユーザー体験を向上する事が出来ます。
例えば、ユーザーがメッセージに返信するなどといった様に、テキスト入力で通知に対して対応する可能性があるなら、ユーザーがウェアラブルバイス音声認識で返信する機能を追加する事ができます。
Android Wearの通知に最高のユーザー体験を提供するのを助けるため、このガイドはNortificationCompartで標準テンプレートを使用して通知を構築する方法を示しています。
APIの構築と、通知のウェアラブルユーザー体験能力の拡張に着手する方法です。
リモートビューを使用した通知はカスタムレイアウトを取り去られ、システムはテキストとアイコンだけをカード内の通知の表示として使います。しかしながら、カスタムカードレイアウトはこの後の公式なAndroid Wear SKDでサポートされるでしょう。
必須クラスの一ポート
開発を始めるには先にGet Started with the Developer Previewの指示に従い完了させておく必要があります。そのドキュメントで述べたように、アプリにはv4 support libraryとDeveloper Preview サポートライブラリが必要です。さて手始めにプロジェクトに次のコードを含めて下さい。
[java light=””true””]
import android.preview.support.wearable.notifications.;
import android.preview.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat;
[/java]
注意:今のAndroid Wear Developer Preview APIは開発及びテスト用途のみを意図しており、アプリケーション構築用ではありません。GoogleAndroid Wear SDKの公式リリースのまえにこのDeveloper Previewを変更する事があります。Developer Previewを使ったアプリケーションを配布したり出荷しないでください。公式なSDKがリリースされた後には、もはやこのDeveloper Previewはサポートされないでしょう(Developer Previewをベースにしているとアプリが壊れる原因となります)。
Create Notifications with the Notification Builder
v4 support libraryは例えばアクションボタンや大きなアイコンなど、最新の通知機能を使った通知をAndroid1.6(API level4)以上との互換性を維持しつつ、作成する事を可能にします。
例として、新しいNortificationManagerCmpart APIと結合されたNortificationCompart APIを使って通知を作成し発行するための幾つかのコードを示します。
 
 
[java light=””true””]
int notificationId = 001;
// 通知コンテンツ用インテントを構築
Intent viewIntent = new Intent(this, ViewEventActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
        PendingIntent.getActivity(this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_event)
        .setContentTitle(eventTitle)
        .setContentText(eventLocation)
        .setContentIntent(viewPendingIntent);
// NotificationManager サービスのインスタンスを得る
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(this);
// 通知の構築と通知マネージャで通知の発行を行う
notificationManager.notify(notificationId, notificationBuilder.build());
[/java]
この通知が携帯端末上に表示されると、ユーザーは通知をタッチする事でsetContentIntent()メソッドで明示されたPendingIntentを呼び出す事が出来ます。
この通知がAndroidウェアラブル上に表示されたとき、ユーザーは通知を左にスワイプしてアクションを開き、そしてそれのインテントを携帯端末上で実行する事が出来ます。
では実際にコーディングしてみましょう。
先ほど作成したプロジェクトにコードを追加していきます。まずはボタンを配置しタップしたら通知を送出するようにします。
まずはボタンを追加。
activity_main.xml
[xml]
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.androidwearnotificationssample.MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button" />
</LinearLayout>
</FrameLayout>
[/xml]
ボタンがタップされたら通知を出します。
[java highlight_lines=””16″” language=”highlight=”0,17-19,34-57"”]
package com.example.androidwearnotificationssample;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.os.Build;
import android.preview.support.wearable.notifications.
;
import android.preview.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        
        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int notificationId = 001;
// 通知コンテンツ用インテントを構築
Intent viewIntent = new Intent(MainActivity.this, ViewEventActivity.class);
PendingIntent viewPendingIntent =
        PendingIntent.getActivity(MainActivity.this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(MainActivity.this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("テスト")
        .setContentText("テストイベントです")
        .setContentIntent(viewPendingIntent);
// NotificationManager サービスのインスタンスを得る
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(MainActivity.this);
// 通知の構築と通知マネージャで通知の発行を行う
notificationManager.notify(notificationId, notificationBuilder.build());
}
});
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    /
     
 A placeholder fragment containing a simple view.
     
/
    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}
[/java]
 
Android Wearで通知をタップした時に開くためのActivityを作成します。
view_event.xml
[xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ViewEventActivity" />
</LinearLayout>
[/xml]
ViewEventActivity.java
[java]
package com.example.androidwearnotificationssample;
import android.app.Activity;
import android.os.Bundle;
public class ViewEventActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_event);
    }
}
[/java]
[xml num=””25″”]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidwearnotificationssample"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidwearnotificationssample.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name="com.example.androidwearnotificationssample.ViewEventActivity" 
            android:label="@string/view_name">
        </activity>
    </application>
</manifest>
[/xml]
実行します。

ボタンをタップすると通知が表示されます。

Android Wearにも通知が表示されます。

 
左にスワイプするとアクションが表示されます。

ボタンをタップすると携帯端末の画面でViewEventActivityが開きます。

Add Action Buttons
setContentIntent()で定義されたプライマリコンテンツアクションに加えて、PendingIntentをaddAction()メソッドで渡す事で別のアクションを追加する事が出来ます。
例えば、次のコードは上からの同じタイプの通知を示していますが、イベントの場所をマップで表示するにはアクションを追加します。
[java light=””true””]
// Build an intent for an action to view a map
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode(location));
mapIntent.setData(geoUri);
PendingIntent mapPendingIntent =
PendingIntent.getActivity(this, 0, mapIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent)
.addAction(R.drawable.ic_map,
getString(R.string.map), mapPendingIntent);
[/java]
携帯端末では、アクションは通知に付けられた追加のボタンとして表示されます。Android Wearableではアクションはユーザが通知を左にスワイプした時に大きなボタンとして表示されます。ユーザがアクションをタップすると関連づけられたインテントが携帯端末上で起動されます。
ヒント:もし通知に「返信」アクションを含める場合には、Android Wearableから直接返信を入力出来るように振る舞いを拡張する事が出来ます。詳細についてはReceiving Voice Input from a Notificationを読んで下さい。
(アイコンの仕様を含む)アクションボタンのデザインの詳細は、Design Principles of Android Wearをご覧下さい。
では実際にコーディングしてみましょう。先ほどの通知に地図を開くアクションを追加します。携帯端末上のボタンを押した時に送信する通知の処理を以下のように修正します。
MainActivity.java
[java 1=”=”0,10-15,23"” language=”highlight_lines”]
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int notificationId = 001;
// 通知コンテンツ用インテントを構築
Intent viewIntent = new Intent(MainActivity.this, ViewEventActivity.class);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(MainActivity.this, 0, viewIntent, 0);
// マップ表示ビューアクション用インテントを構築
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri geoUri = Uri.parse("geo:0,0?q=Osaka");
mapIntent.setData(geoUri);
PendingIntent mapPendingIntent =
PendingIntent.getActivity(MainActivity.this, 0, mapIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("テスト")
.setContentText("テストイベントです")
.setContentIntent(viewPendingIntent)
.addAction(drawable.ic_dialog_map, getString(R.string.map), mapPendingIntent);
// NotificationManager サービスのインスタンスを得る
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(MainActivity.this);
// 通知の構築と通知マネージャで通知の発行を行う
notificationManager.notify(notificationId, notificationBuilder.build());
}
});
[/java]
Add a Big View
“big view”スタイルのうち1つを通知に追加する事で、拡張したテキストコンテンツを通知に挿入する事が出来ます。携帯デバイスでは、通知を拡張したbig view コンテンツを見る事ができ、同時にAndroid Wearではbig viewコンテンツはデフォルトで表示されます。
通知に拡張されたコンテンツを追加するには、NotificationCmpat.Builder オブジェクトでsetStyle()を呼び出し、BigTextStyle またはinboxStyleのインスタンスを渡します。
例えば、次のコードは、完全なイベントの詳細を含めるために、イベント通知にNortificationCompat.BigTextStyleのインスタンスを追加しています(setContentText()へ提供されるスペースに収まるよりも多くのテキストを含みます)。
[java light=””true””]
// 通常のテキストではフィットしないような長いイベントの説明を
// 表示する場合は、big viewコンテンツを指定します。
BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(eventDescription);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setLargeIcon(BitmapFractory.decodeResource(
getResources(), R.drawable.notif_background))
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent)
.addAction(R.drawable.ic_map,
getString(R.string.map), mapPendingIntent)
.setStyle(bigStyle);
[/java]
では実際にコーディングしてみましょう。先ほどの通知をbig styleにしてみます。携帯端末上のボタンを押した時に送信する通知の処理を以下のように修正します。
[java]
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int notificationId = 001;
// 通知コンテンツ用インテントを構築
Intent viewIntent = new Intent(MainActivity.this, ViewEventActivity.class);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(MainActivity.this, 0, viewIntent, 0);
// マップ表示ビューアクション用インテントを構築
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri geoUri = Uri.parse("geo:0,0?q=Osaka");
mapIntent.setData(geoUri);
PendingIntent mapPendingIntent =
PendingIntent.getActivity(MainActivity.this, 0, mapIntent, 0);
// 通常のテキストではフィットしないような長いイベントの説明を
// 表示する場合は、big viewコンテンツを指定します。
BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText("通常のテキストではフィットしないような長いイベントの説明を表示する場合は、big viewコンテンツを指定します。");
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("テスト")
.setContentText("テストイベントです")
.setContentIntent(viewPendingIntent)
.addAction(drawable.ic_dialog_map, getString(R.string.map), mapPendingIntent)
.setStyle(bigStyle);
// NotificationManager サービスのインスタンスを得る
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(MainActivity.this);
// 通知の構築と通知マネージャで通知の発行を行う
notificationManager.notify(notificationId, notificationBuilder.build());
}
});
[/java]
実行してみましょう。ボタンをタップすると通知が表示されます。

最初は省略表示されていますが、タップすると、通知領域が広がります。

 
ウェアラブルのための新しい機能を追加する
Android Wear preview support library はウェアラブルバイス上の通知のユーザー体験を拡張することができる新しいAIPを提供しています。例えば、ユーザーがビューを左にスワイプして表示できる追加のコンテンツのためのページを追加したり、ユーザーがアプリが提供したテキストに音声入力で応答するといった機能を追加できます。
これらの新しいAPIを使うにはWearableNotifications.Builder()コンストラクタにNortificationCompat.Builderのインスタンスを渡します。それからWearableNotifications.Builderメソッドに通知に新しい機能を追加します。
例えば、次のように、
[java]
// 通常の通知機能用にNotificationCompat.Builderを作成します。
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail);
// ウェアラブル用に独自機能を追加するために WearablesNotification.Builder を作成します。
Notification notification =
new WearableNotifications.Builder(notificationBuilder)
.setHintHideIcon(true)
.build();
[/java]
setHintHideIcon()メソッドは通知カードからアイコンを削除します。このメソッドはWearableNotifications.Builderクラスから利用できる新しい通知機能の一つです。
あなたの通知を配信したい場合、必ずNotificationManagerCompat APIを使うように注意して下さい。
[java]
// NotificationManagerサービスのインスタンスを取得します
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// 通知マネージャーで通知を作成し発行します
notificationManager.notify(notificationId, notification);
[/java]
代わりにフレームワークの通知マネージャーを使ってしまうと、WearableNotifications.Builderの一部の機能は動作しません。
それではプロジェクトにコードを追加してみましょう。MainViewにボタンを追加し、コードを追加します。
[java]
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int notificationId = 002;
// 通常の通知機能用にNotificationCompat.Builderを作成します。
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("New mail from テスト")
.setContentText("これはテストメールです")
.setSmallIcon(drawable.ic_dialog_email);
// ウェアラブル用に独自機能を追加するために WearablesNotification.Builder を作成します。
Notification notification =
new WearableNotifications.Builder(notificationBuilder)
.setHintHideIcon(true)
.build();
// NotificationManagerサービスのインスタンスを取得します
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(MainActivity.this);
// 通知マネージャーで通知を作成し発行します
notificationManager.notify(notificationId, notification);
}
});
[/java]
実行し携帯端末上でボタンをタップすると通知が発行されます。
ウェアラブル上ではアイコンが非表示になっている事が確認できます。

 
WearableNotifications.Builder およびその他の API を使用してウェアラブルの通知をさらに強化するには、次の開発者ガイドを参照してください。
通知から音声入力を受け取る
ユーザーからの音声入力を受けとり、変換されたメッセージをアプリケーションへ配信するアクションを追加します。
通知にページを追加する
ユーザーが左にスワイプした時に表示する追加情報ページを追加します。
通知を積み重ねる
スタック内でアプリからの同じような通知が、カードストリームに複数のカードを追加する事なく個々に見る事が出来るように配置します。
さて、本日はここまでとしてまた明日にしましょう。
 

「GoogleがスマートウォッチOS “Android Wear” を発表したのでテスターになってみた」への2件のフィードバック

  1. andoroidのウェアラブルについて調べていたら、コノサイトにたどりつきまたした。
    開発はやっていませんが、andoroid4.2のスマフォでandoroid wearをインストールしたとの記事でたどり着きました。
    com.google.android.wearablepreview.app-1.0.0.1077241.apkのリンク先はGoogleでアクセス禁止になっており、アプリが手に入りません。お手持ちまたはサイトのリンクをご教示願いたくメールしました。

  2. 残念ですが、Android WearはAndroid4.3から搭載されたBLEを使用します。そのためAndroid4.3は必須ですね。本記事では開発のために無理やりインストールしていますが、開発時はBLE通信しないために実現している事であり、実際の機器では動作しませんよ。

コメントは受け付けていません。

上部へスクロール