从 wear 启动应用程序并获取消息,无论应用程序的状态如何

Launch app from wear and get the message regardless the state of app

我是做可穿戴的,我的目的是下一个:

我想在我的手表上按下一个简单的按钮,向手机发送一条简单的消息。但我想处理所有这些行为:

到目前为止,我处理了在应用程序尚未启动时启动应用程序,但我无法在启动器 activity 中收到 额外消息 包含在 意图 中。代码在这里。

移动服务

public class MobileWearService extends WearableListenerService {

    private static final String START_ACTIVITY = "/start_activity";

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        super.onMessageReceived(messageEvent);
        String event = messageEvent.getPath();
        String msg = new String(messageEvent.getData());

        if (event.equals(START_ACTIVITY)) {
            Intent intent = new Intent( this, MainActivity.class );
            intent.putExtra("Data", msg);
            intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK
                               | Intent.FLAG_ACTIVITY_CLEAR_TOP
                               | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity( intent );
        }
    }
}

但是,如果我使用广播将消息从服​​务发送到主 activity,它仅在应用程序启动并在前台时有效

   public class MobileWearService extends WearableListenerService {

        private static final String START_ACTIVITY = "/start_activity";

        @Override
        public void onMessageReceived(MessageEvent messageEvent) {
            super.onMessageReceived(messageEvent);
            String event = messageEvent.getPath();
            String msg = new String(messageEvent.getData());

            if (event.equals(START_ACTIVITY)) {
                broadcastIntent.setAction("com.me.project.wear.to.app");
                broadcastIntent.putExtra("Data", msg);
                broadcastIntent.putExtras(intent);
                sendBroadcast(broadcastIntent);
            }
        }
    }

启动器activity

private IntentFilter mIntentFilter = new IntentFilter("com.me.project.wear.to.app");
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && intent.getAction().equals("com.me.project.wear.to.app")) {
            String msg = intent.getStringExtra("Data");
        }
    }
};

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(mReceiver, mIntentFilter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mReceiver);
}

所以我想结合这个事实从磨损中获取消息(我知道如何)但是传递这个消息以在启动器中获取它 activity 无论应用程序的状态如何。

只需制作静态 BroadcastReceiver

public class WatchMessageReceiver extends BroadcastReceiver{

@Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null &&  
            intent.getAction().equals("com.me.project.wear.to.app")) {
            String msg = intent.getStringExtra("Data");
            Intent launcherIntent = new Intent(context, LauncherActivity.class);
            launcherIntent.putExtra("Data",msg);
            startActivity(launcherIntent);
        }
    }
}

在您的清单文件中

   <receiver android:name ="WatchMessageReceiver"
      <intent-filter>
         <action android:name="com.me.project.wear.to.app"/>
      </intent-filter>
  </receiver>

Sending and Syncing Data training, there is a Handling Data Layer Events中:

When you make a call to the Data Layer API, you can receive the status of the call when it completes. You also can listen for data events, resulting from data changes that your application makes anywhere on the Android Wear network.

侦听数据层事件

Because the data layer synchronizes and sends data across the handheld and wearable, it is usually necessary to listen for important events. Examples of such events include creation of data items and receipt of messages.

To listen for data layer events, you have two options:

  • Create a service that extends WearableListenerService.
  • Create an activity that implements DataApi.DataListener.

With both these options, you override the data event callback methods for the events you are interested in handling.

您可以使用 WearableListenerService 监听的一些事件如下:

  • onDataChanged(): Whenever a data item object is created, deleted, or changed, the system triggers this callback on all connected nodes.
  • onMessageReceived(): A message sent from a node triggers this callback on the target node.
  • onCapabilityChanged(): When a capability that an instance of your app advertises becomes available on the network, that event triggers this callback. If you're looking for a nearby node you can query the isNearby() method of the nodes provided in the callback.

根据相关的 SO post:

WearableListenerService does not run constantly - it is only started when a new message/node connection/data layer change is sent and stopped when there are no more messages.

希望这对您有所帮助。