其他分享
首页 > 其他分享> > Android Wear应用无法连接到Google API服务

Android Wear应用无法连接到Google API服务

作者:互联网

我正在尝试在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>

解决方法:

Android Wear要求您在手机上安装Google Play服务版本> = 5.0.89

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

也有用:

Important: Because it is hard to anticipate the state of each device,
you must always check for a compatible Google Play services APK before
you access Google Play services features. For many apps, the best time
to check is during the onResume() method of the main activity.

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

// ########################################################################
// 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

标签:android,google-play-services,wear-os
来源: https://codeday.me/bug/20191010/1884802.html