かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

AndroidからAndroidWearのアプリを起動する

基本的には、これに準じます。

上記ページがAndroid Wear → AndroidなのをAndroid → Android WearにしてやればOK。

Android Studioでプロジェクトを作ってMobile側のアプリでメニューのSettingsをタップしたときの処理あたりに、メッセージ送信処理を突っ込みます。

package com.example.kazuki.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.Wearable;


public class MyActivity extends Activity {

    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        this.client = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .build();
        this.client.connect();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, 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) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(client).await();
                    for (Node node : nodes.getNodes()) {
                        MessageApi.SendMessageResult r = Wearable.MessageApi.sendMessage(
                                client,
                                node.getId(),
                                "/path",
                                "Hello world".getBytes()
                        ).await();
                    }
                }
            }).start();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

GoogleApiClientを使うのでAndroidManifest.xmlに以下の定義を追加します。

 <meta-data
     android:name="com.google.android.gms.version"
     android:value="@integer/google_play_services_version" />

Android Wear側では、WearableListenerServiceを継承したサービスを作って、メッセージを受信したらIntentを使ってActivityを起動するようにします。サービスから起動するのでIntentのフラグにFLAG_ACTIVITY_NEW_TASKを追加しておきます。

package com.example.kazuki.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;

public class MyService extends WearableListenerService {
    public MyService() {
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Intent i = new Intent(this, MyActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(i);
    }
}

サービスの定義をしておきます。intent-filterが特殊なので忘れずに。

<service
    android:name=".MyService" >
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
    </intent-filter>
</service>