【问题标题】:This code keeps telling me I need to update Google Play Services. How do I fix that?此代码不断告诉我我需要更新 Google Play 服务。我该如何解决?
【发布时间】:2015-12-07 14:23:43
【问题描述】:

这是我的 MainActivity.java。它一直说我需要更新 google play 服务,但我以为我已经在 build.gradle 中安装了它们。如果您发现问题,请告诉我。

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;


public class MainActivity extends ActionBarActivity  implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

private static int REQUEST_CODE_RECOVER_PLAY_SERVICES = 200;

private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private LocationRequest mLocationRequest;


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

    if (checkGooglePlayServices()) {
        buildGoogleApiClient();

        //prepare connection request
        createLocationRequest();
    }
}






private boolean checkGooglePlayServices() {

    int checkGooglePlayServices = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);
    if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
          /*
           * google play services is missing or update is required
           *  return code could be
           * SUCCESS,
           * SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED,
           * SERVICE_DISABLED, SERVICE_INVALID.
           */
        GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices,
                this, REQUEST_CODE_RECOVER_PLAY_SERVICES).show();

        return false;
    }

    return true;

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_RECOVER_PLAY_SERVICES) {

        if (resultCode == RESULT_OK) {
            // Make sure the app is not already connected or attempting to connect
            if (!mGoogleApiClient.isConnecting() &&
                    !mGoogleApiClient.isConnected()) {
                mGoogleApiClient.connect();
            }
        }else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Google Play Services must be installed.",
                    Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

}


@Override
public void onConnected(Bundle bundle) {

    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {

        Toast.makeText(this, "Latitude:" + mLastLocation.getLatitude()+", Longitude:"+mLastLocation.getLongitude(),Toast.LENGTH_LONG).show();

    }

    startLocationUpdates();

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}


/* Second part*/

@Override
protected void onStart() {
    super.onStart();

    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    }

}

protected void startLocationUpdates() {
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(20000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    Toast.makeText(this, "Update -> Latitude:" + mLastLocation.getLatitude()+", Longitude:"+mLastLocation.getLongitude(),Toast.LENGTH_LONG).show();

}

protected void stopLocationUpdates() {
    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(
                mGoogleApiClient, this);
    }
}

@Override
protected void onPause() {
    super.onPause();
    stopLocationUpdates();
}

@Override
protected void onStop() {
    super.onStop();

    if (mGoogleApiClient != null) {
        mGoogleApiClient.disconnect();
    }


}
}

这是我的 build.gradle 文件夹:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.example.usinglocationiapi.usinglocationapi"
    minSdkVersion 23
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.google.android.gms:play-services:8.3.0'



}

【问题讨论】:

  • AFAIK isGooglePlayServicesAvailable 将检查设备上的 Google Play 服务版本是否为最新版本,如果不是最新版本则返回错误。然后getErrorDialog 将返回要求用户更新 Google Play 服务的对话框。不确定您是否可以对此做任何事情,因为 Google 可能会继续发布新的更新。请参阅this page,了解 Google Play 服务客户端库和 Google Play 服务 APK 如何组合在一起。

标签: android gradle location google-play-services


【解决方案1】:

8.3.0 不是 google play 服务的最新版本。将 8.3.0 更改为 8.4.+

编辑:好的,试试这个,转到工具 - Android - SDK 管理器并更新您的 google play services sdk。我不确定它是否能解决您的问题,但请尝试一下..

【讨论】:

  • 我尝试将其更改为 8.4.0 和 8.4.+,但 gradle 构建两次都失败了,说它无法下载存储库。你能告诉我在这种情况下功能性的 gradle 文件夹会是什么样子吗?
  • 有一个可用的更新,但复选框中间有一条黑线。当我点击应用时,它不起作用。
  • @felix 单击复选框,直到它出现如图所示的勾号。然后按确定,它会告诉你它会更新它..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 2018-02-28
  • 1970-01-01
相关资源
最近更新 更多