我想从穿戴设备向Android手机发送消息,穿戴设备中的“ SendMessageResult”是成功的,但不会触发我的手机上的“ showToast”方法,有人可以帮我了解我在做什么?

这是我的穿衣清单

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

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.DeviceDefault">
    <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>
<meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

</manifest>


这是我的手机清单

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <service android:name=".Lisswear">
        <intent-filter>
            <!-- listeners receive events that match the action and data filters -->
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <data android:scheme="wear" android:host="*" android:pathPrefix="/showToast" />
        </intent-filter>
    </service>

</application>

</manifest>


穿戴的MainActivity.java

package com.example.user.wear2;

import android.app.Activity;
import android.content.IntentSender;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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

import java.io.UnsupportedEncodingException;

public class MainActivity extends Activity {

private TextView mTextView;
private Button btsent;
private boolean mbResulvingGooApiClientError = false;
private static final int GOO_API_CLIENT_REQUEST_ERROR = 1000;
private static final String Msg_Path = "/message";

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

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(gooApiClientConnCallback)
            .addOnConnectionFailedListener(gooApiClientOnConnFailed)
            .build();

    final WatchViewStub stub = (WatchViewStub)                findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
            btsent =  (Button) stub.findViewById(R.id.button);
        }
    });
}

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

@Override
protected void onStop() {
    super.onStop();
    if(!mbResulvingGooApiClientError){
        Wearable.MessageApi.removeListener(mGoogleApiClient,wearableMsgListener);
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

public void onclickbtsent(View view){
    new AsnyTaskSendMessageToWeaarableDevice().execute();
}

private GoogleApiClient.ConnectionCallbacks gooApiClientConnCallback = new GoogleApiClient.ConnectionCallbacks(){
    @Override
    public void onConnected(@Nullable Bundle bundle) {
        mbResulvingGooApiClientError = false;
        Wearable.MessageApi.addListener(mGoogleApiClient,wearableMsgListener);
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d("onConnectionSuspended","0");
    }
};

private GoogleApiClient.OnConnectionFailedListener gooApiClientOnConnFailed  = new GoogleApiClient.OnConnectionFailedListener(){

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        if(mbResulvingGooApiClientError){
            return;
        }else if(connectionResult.hasResolution()){
            try{
                mbResulvingGooApiClientError = true;
                connectionResult.startResolutionForResult(MainActivity.this,GOO_API_CLIENT_REQUEST_ERROR);
            }catch (IntentSender.SendIntentException e){
                mbResulvingGooApiClientError = false;
                mGoogleApiClient.connect();
            }
        }else{
            mbResulvingGooApiClientError = false;
            Wearable.MessageApi.removeListener(mGoogleApiClient,wearableMsgListener);
        }
    }
};

private MessageApi.MessageListener wearableMsgListener = new MessageApi.MessageListener(){

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Log.d("onMessageReceived", String.valueOf(messageEvent.getData()));
    }
};

private class AsnyTaskSendMessageToWeaarableDevice extends AsyncTask {

    @Override
    protected Object doInBackground(Object[] objects) {
        NodeApi.GetConnectedNodesResult connectedWearableDevices = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
        for(Node node: connectedWearableDevices.getNodes()){
            String str1 = "Udemy online courses";
            try {
                byte[] arr = str1.getBytes("UTF-8");
                //String str2 = new String(arr);
                //System.out.println("new string = "+ str2);
                Log.d("node and DisplayName = ",node.getId()+ " - " + node.getDisplayName());
                MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), Msg_Path, arr).await();
                if(result.getStatus().isSuccess()){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("result.getStatus","Success");
                        }
                    });
                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("result.getStatus","Error");
                        }
                    });
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }
        return null;
    }
}


}


手机的MainActivity .java

package com.example.user.wear2;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Wearable;

public class MainActivity extends AppCompatActivity  implements    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;
private static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.d(TAG,"onConnected");
}

@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG,"onConnectionSuspended");
}

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


这是Lisswear.java,我在移动设备中的WearableListenerService

package com.example.user.wear2;

import android.util.Log;
import android.widget.Toast;

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

public class Lisswear extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
    //if(messageEvent.getPath().equals("/showToast")) {
    showToast(messageEvent.getPath());
    //}
}


private void showToast(String message) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}

最佳答案

您可以尝试以下操作:


确保两个设备上的applicationid都与此SO thread中的情况相同
确保您使用相同的包裹名称
确保您有enabled permissions
您还可以在文档中从可穿戴设备到设备阅读有关Sending and Receiving Messages的更多信息。

10-08 04:53