LoginSignup
7
6

More than 5 years have passed since last update.

XamarinでAndroid Wearサンプルを動かしてみる その2

Last updated at Posted at 2014-04-16

前回に引き続きAndroid WearのサンプルをXamarinで動かしてみます。
今回はサンプルの一つ RecipeAssistant をXamarin版に書き換えました。ソースコードはGitHubにあります。

RecipeAssistantとは

前回のサンプルはAndroid Wearの使い方を知るためのリファレンス的なアプリでした。
今回のRecipeAssistantはアプリになっているので、この中でAndroid Wearがどのように使われているを理解のために簡単にメモしていきます。

  • Android端末側
    Recipe Assistant

  • Android Wear側
    Recipie Assistant

このアプリは上記の画像のようにアプリ側でレシピを選択し見ることができます。
そして、レシピの作成手順をAndroid Wearに飛ばせるようになっています。

実装部分

今回の肝はサンプル内の RecipeSerivce クラスになります。このサービスクラスがAndroid Wearを利用した通知を行っています。実装部分は次のようになっています(Recipieクラスに料理の種々の情報が格納されている)。

RecipeService.cs
private void CreateNotification(Intent intent) {
    this.mRecipe = Recipe.FromBundle (intent.GetBundleExtra (Constants.EXTRA_RECIPE));
    var notificationPages = new List<Notification> ();

    int stepCount = mRecipe.RecipeSteps.Count;
    for (int i = 0; i < stepCount; ++i) {
        var recipeStep = mRecipe.RecipeSteps [i];
        // レシピのステップごとのページのスタイルを作成する
        var style = new NotificationCompat.BigTextStyle ();
        style.BigText (recipeStep.StepText);
        style.SetBigContentTitle (Resources.GetString (Resource.String.StepCount, i + 1, stepCount));
        style.SetSummaryText ("");

        // ページ用の通知を作成する
        var builder = new NotificationCompat.Builder (this)
                                .SetStyle (style);
        notificationPages.Add (builder.Build ());
    }

    {
        var builder = new NotificationCompat.Builder(this);

        // レシピに画像がある場合は通知の背景用に設定する
        if (mRecipe.RecipeImage != null) {
            var recipeImage = Bitmap.CreateScaledBitmap(
                AssetUtils.LoadBitmapAsset(this, mRecipe.RecipeImage),
                Constants.NOTIFICATION_IMAGE_WIDTH, Constants.NOTIFICATION_IMAGE_HEIGHT, false);
            builder.SetLargeIcon(recipeImage);
        }
        builder.SetContentTitle(mRecipe.TitleText)
                .SetContentText(mRecipe.SummaryText)
                .SetSmallIcon(Resource.Drawable.NotificationRecipe);

        // WearableNotifications.Builderクラスを利用し、Android Wear用のページ通知を作成し、発行する
        var notification = new WearableNotifications.Builder(builder)
                .AddPages(notificationPages)
                .Build();
        mNotificationManager.Notify(Constants.NOTIFICATION_ID, notification);
    }
}

RecipieAssistantアプリではページ型の通知を出しています。ページを作成するために、複数のNotificationを作成しているような形になっています。
このサンプルのAndroid Wearを利用している部分は上記のメソッド1つのみになります。

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6