onMessageReceived()从未调用过,虽然连接到掌上电脑

时间:2015-03-20 19:37:39

标签: android events listener call message

我希望仅在收到消息时才将消息从手持设备发送到智能手表(Moto360)。

我确保两个模块具有相同的软件包名称,debug.key和版本号。

侦听器被添加到onConnected()中,并且始终调用该函数。

这是我的掌上电脑gradle.build:

apply plugin: 'com.android.application'

android {
compileSdkVersion 19
buildToolsVersion "22.0.0"

defaultConfig {
    applicationId "de.bachelorthesis.important"
    minSdkVersion 9
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
}

signingConfigs {
    debug {
        storeFile file("../debug.keystore")
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
  }
}

dependencies {
compile 'com.android.support:support-v4:19.1.0'
compile 'com.google.android.gms:play-services:7.0.0'
compile files('libs/commons-io-2.4.jar')
compile files('libs/mapquest-android-sdk-1.0.5.jar')
compile files('libs/osmdroid-android-4.2.jar')
compile files('libs/slf4j-android-1.7.7.jar')
wearApp project(':wear')

}

在我的掌上电脑活动中,邮件应该像这样发送:

 private void sendToSmartwatch(msg) {
    final String msg_arg = msg;

    new Thread(new Runnable() {
        public void run() {

            mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
                    .addApi(Wearable.API)
                    .build();

            ConnectionResult connectionResult =
                    mGoogleApiClient.blockingConnect(5, TimeUnit.SECONDS);

            NodeApi.GetLocalNodeResult nodes =
                    Wearable.NodeApi.getLocalNode(mGoogleApiClient).await(3, TimeUnit.SECONDS);


            com.google.android.gms.wearable.Node node = nodes.getNode();

            MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
                    mGoogleApiClient, node.getId(), CONSTANT, msg_arg.getBytes()).await();
            Log.d("node:result: ", "" + result.getStatus());


            if (result.getStatus().isSuccess()) {
                // Log success
            }
            else {
                // Log an error
            }

            mGoogleApiClient.disconnect();
        }
    }).start();
}

Wearable gradle.build文件如下所示:

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"

defaultConfig {
    applicationId "de.bachelorthesis.important"
    minSdkVersion 20
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}

signingConfigs {
    debug {
        storeFile file("../debug.keystore")
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.android.support:wearable:1.1.0'
compile 'com.google.android.gms:play-services-wearable:+'
compile 'com.google.android.gms:play-services:+'
compile 'com.android.support:support-v4:22.0.0'
}

最终,这条消息应该出现在我的手表上:

public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

...

onCreate(){

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}

mListener = new MessageApi.MessageListener() {
        @Override
        public void onMessageReceived(MessageEvent messageEvent) {
            if (messageEvent.getPath().equals(CONSTANT)) {
                //do stuff with the message
            }
         }
    };

}


@Override
public void onConnected(Bundle bundle) {
    Wearable.MessageApi.addListener(mGoogleApiClient, mListener);
}

我尝试了几个不同的监听器,例如ListenerService(还有manifest.xml中的BIND_LISTENER)

我也尝试过PhoneService Class from this answer

有什么建议我做错了吗?

编辑:我忘了提到邮件是从手持设备成功发送的,但是没有收到。

编辑2:这可能是一个问题,我的掌上电脑模块的名称是“app”而不是“mobile”?!此外,它是一个从eclipse迁移的项目,用于添加可穿戴功能。

1 个答案:

答案 0 :(得分:0)

所以,我自己想出来了:

This project正在帮助。

我认为常数有所不同。这不是路径,而是我随机设定的常数。仍然不确定,哟。