简体   繁体   English

无法将磨损信息发送到我的手机应用程序

[英]can't send message from wear to my phone app

I want to send data from my Wear to the PhoneApp. 我想将数据从Wear发送到PhoneApp。 I created a phone app with this AndroidManifest.xml 我使用此AndroidManifest.xml创建了一个电话应用

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="sh.evolutio.car">

    <uses-feature
        android:name="android.software.leanback"
        android:required="true" />

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service android:name=".services.ListenerService" >
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
                <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
                <!-- <data android:scheme="wear" android:host="*" android:pathPrefix="/updatecar" /> -->
            </intent-filter>
        </service>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

my ListenerService: 我的ListenerService:

package sh.evolutio.car.services;

import android.content.Intent;
import android.util.Log;

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

public class ListenerService extends WearableListenerService {
  private static final String TAG = "ListenerService";
  private static final String MESSAGE_PATH = "/updatecar";

  @Override
  public void onCreate() {
      Log.d(TAG, "ListenerService created");
  }

  @Override
  public void onMessageReceived(MessageEvent messageEvent) {
      Log.d(TAG, "onMessageReceived");

      if (messageEvent.getPath().equals(MESSAGE_PATH)) {
          Log.d(TAG, "good message");
      } else {
          Log.d(TAG, "bad message");
      }
  }
}

my MainActivity with this onCreate: 我的MainActivity与此onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);

  startService(new Intent(MainActivity.this, ListenerService.class));
}

When I start the App on my phone I got in the Logcat: 当我在手机上启动应用程序时,我进入了Logcat:

26131-26131/sh.evolutio.car D/ListenerService: ListenerService created

When I send with the wearapp some data to my phone, my ListenerService didn't fire the onMessageReceived method.. 当我通过wearapp发送一些数据到我的手机时,我的ListenerService没有触发onMessageReceived方法。

Here is my AndroidManifest from the wearapp: 这是我来自wearapp的AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="sh.evolutio.carwear">

  <uses-feature android:name="android.hardware.type.watch" />

  <uses-permission android:name="android.permission.WAKE_LOCK" />

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.DeviceDefault">
    <uses-library
      android:name="com.google.android.wearable"
      android:required="true" />
    <meta-data
      android:name="com.google.android.wearable.standalone"
      android:value="false" />

    <activity
      android:name=".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>
  </application>
</manifest>

The MainActivity from the wearapp looks like this: wearapp的MainActivity如下所示:

package sh.evolutio.carwear;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.wearable.activity.WearableActivity;
import android.util.Log;
import android.view.View;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
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 MainActivity extends WearableActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  private static String TAG = "MainActivity";
  private static final String MESSAGE_PATH = "/updatecar";

  Node mNode; // the connected device to send the message to
  GoogleApiClient mGoogleApiClient;
  private boolean mResolvingError = false;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Connect the GoogleApiClient
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    sendMessage("test");

    // Enables Always-on
    setAmbientEnabled();
  }

  @Override
  protected void onStart() {
    super.onStart();
    if (!mResolvingError) {
      mGoogleApiClient.connect();
    }
  }

  /**
    * Resolve the node = the connected device to send the message to
    */
  private void resolveNode() {
    Log.d(TAG, "resolveNode");

    Wearable.NodeApi.getConnectedNodes(mGoogleApiClient)
      .setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
        @Override
        public void onResult(NodeApi.GetConnectedNodesResult nodes) {
          for (Node node : nodes.getNodes()) {
            Log.d(TAG, "resolvedNode: " + node);
            mNode = node;
          }
        }
      });
  }

  @Override
  public void onConnected(Bundle bundle) {
    resolveNode();
  }

  @Override
  public void onConnectionSuspended(int i) {}

  @Override
  public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d(TAG, "connectionResult: " + connectionResult);
  }

  /**
    * Send message to mobile handheld
    */
  private void sendMessage(String Key) {
    if (mNode != null && mGoogleApiClient!= null && mGoogleApiClient.isConnected()) {
      final String messageKey = Key;

      Log.d(TAG, "isConnected: " + mGoogleApiClient.isConnected());
      Log.d(TAG, "connected to: " + mNode.getId());

      Task<Integer> sendTask = Wearable.getMessageClient(MainActivity.this).sendMessage(mNode.getId(), MESSAGE_PATH, messageKey.getBytes());
      sendTask.addOnSuccessListener(new OnSuccessListener<Integer>() {
        @Override
        public void onSuccess(Integer integer) {
          Log.d(TAG, "onSuccess: " + integer);
        }
      });

      sendTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
          Log.d(TAG, "onFailure: " + e.getMessage());
        }
      });

      Wearable.MessageApi.sendMessage(mGoogleApiClient, mNode.getId(), MESSAGE_PATH, messageKey.getBytes()).setResultCallback(
        new ResultCallback<MessageApi.SendMessageResult>() {
          @Override
          public void onResult(@NonNull MessageApi.SendMessageResult sendMessageResult) {
            if (sendMessageResult.getStatus().isSuccess()) {
              Log.v(TAG, "Message: { " + messageKey + " } sent to: " + mNode.getDisplayName());
            } else {
              // Log an error
              Log.v(TAG, "ERROR: failed to send Message");
            }
          }
        }
      );
    }
  }
}

When I the message got send, i got this in the logcat from the wearapp: 当我收到消息后,我从wearapp的logcat中得到了消息:

sh.evolutio.carwear D/MainActivity: isConnected: true
sh.evolutio.carwear D/MainActivity: connected to: 778d0d53
sh.evolutio.carwear V/MainActivity: Message: { forward } sent to: HUAWEI Mate 10 Pro
sh.evolutio.carwear D/MainActivity: onSuccess: 17282

so the message was sent to my Mate 10 Pro. 因此邮件已发送到我的Mate 10 Pro。 But why my Mate 10 Pro App can't receive the Message? 但是,为什么我的Mate 10 Pro App无法收到该消息? Where is my mistake? 我的错误在哪里? I didn't find it. 我没找到

In your mobile activity, do not start the service manually. 在您的移动活动中,请勿手动启动服务。 The service will be started automatically by Android on reception of the message. 收到消息后,Android将自动启动该服务。 And you need to uncomment your data pathPrefix definition in the AndroidManifest. 并且您需要在AndroidManifest中取消注释数据路径前缀定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM