Android Wear app无法连接到google api服务

时间:2014-09-16 10:51:32

标签: android google-play-services wear-os

我试图在Android Wear和Android手机之间建立连接。 googleApiClient无法连接,并返回SERVICE_VERSION_UPDATE_REQUIRED状态代码。 我做错了什么?

这是我的活动代码:

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

    int conResult = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
    if(conResult!=ConnectionResult.SUCCESS){
        Log.e(LOG_TAG,"Google play services are not available on this device");
    }

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

这是在wear文件夹上的build.gradle文件:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 20
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    //compile 'com.google.android.gms:play-services-wearable:+'
    compile 'com.google.android.support:wearable:+'
    compile 'com.android.support:appcompat-v7:20.+'
    compile 'com.google.android.gms:play-services:5.+'

}

我的AndroidManifest.xml:

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

<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="android.permission.BODY_SENSORS" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

</manifest>

2 个答案:

答案 0 :(得分:1)

Android Wear要求您在手机上安装Google Play服务版&gt; = 5.0.89

您可以在此检查您的版本并更新Play服务APK: https://play.google.com/store/apps/details?id=com.google.android.gms&hl=en

也很有用:

  

重要提示:因为很难预测每个设备的状态,   您必须先检查兼容的Google Play服务APK   您可以访问Google Play服务功能。对于许多应用程序,最好的时间   检查是在主活动的onResume()方法期间。

检查播放服务是否可用的代码(在您的手机上):

// ########################################################################
// Check if Google Play Services is installed. If not show error dialog.
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if( ConnectionResult.SUCCESS == result ){
    mGoogleApiClient.connect();
    loadingTextView.setText("Connecting to Wear...");
}
else {
    // Show appropriate dialog
    Dialog d = GooglePlayServicesUtil.getErrorDialog(result, this, 0);
    d.show();
}

此处有更多详情:

https://developer.android.com/google/play-services/setup.html#ensure

答案 1 :(得分:0)

在onStart()上放置googleClient.connect()似乎解决了这个问题:

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

    // Build a new GoogleApiClient
    checkIfGoogleApiClientExists();
    googleClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

@Override
protected void onStart() {
    Log.i(LOG_TAG,"entered onStart");
    super.onStart();
    googleClient.connect();
}