【问题标题】:Android wear app can't connect to google api servicesAndroid Wear 应用无法连接到 google api 服务
【发布时间】:2014-09-16 10:51:31
【问题描述】:

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

【问题讨论】:

  • 您可以查看我的问题:stackoverflow.com/questions/35786083/… 在我使用新版本的 Play Service sdk 时 -> 无法连接 Google Api 客户端。我应该使用 6.5.87 版本;
  • 我认为问题可能与安卓模拟器有关

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


【解决方案1】:

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

您可以在此处查看您的版本并更新 Play Services 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

【讨论】:

  • 谢谢。确保我在Android Wear(相对于Android)上有一定的apk有点问题
  • @Omri374 Android 会为您检查版本。修复:如果 ConnectionResult 为 != SUCCESS Dialog d = GooglePlayServicesUtil.getErrorDialog(result, this, 0); 则显示更新对话框
  • 在屏幕上显示错误并不能解决问题...我的设置有一些错误导致它无法正常工作。
【解决方案2】:

看起来将 googleClient.connect() 放在 onStart() 上可以解决问题:

@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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 2019-02-17
    相关资源
    最近更新 更多