Android Wear: Starting a service on Handheld

断了今生、忘了曾经 提交于 2019-12-08 16:39:06

问题


I'm building a Wear app that will communicate with a WearableListenerService on the handheld. However, i want to make sure that service is up and running when the app starts on the watch.

My initial thought was either send an intent or a broadcast message to get the service started. However, i've been unable to figure out how to get the watch to send that to the paired handheld instead.

On the watch side:

Intent intent = new Intent();
intent.setAction("my.wearable.CONNECT");
sendBroadcast(intent);

On the handheld side:

public class WearableBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, WearableService.class);
        context.startService(startServiceIntent);
    }
}

<receiver android:name=".service.wearable.WearableBroadcastReceiver" >
    <intent-filter>
        <action android:name="my.wearable.CONNECT" />
    </intent-filter>
</receiver>

回答1:


There are couple of things that need to clear

  1. The communication interfaces between an Android Wear Device and an Android Device are the following

    • DataApi
    • MessageApi

    Use NodeApi to get the connected Node ID.

  2. You cannot send a intent from wear side and expect to receive it on the phone side.

  3. On the phone side, if you are extending WearableListenerService then this service is self managed by Android OS. Meaning, if the service were to receive a message or data from wear, Android would automatically create the service, service your request and destroy the service if required. You do not have to do any special thing here.

The intent defined in the manifest (copy pasted above) is good enough for Android OS to do the above stated management of the service.



来源:https://stackoverflow.com/questions/26656252/android-wear-starting-a-service-on-handheld

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!