在 Android 设备中以编程方式安装“Google Play 服务”

And*_*iac 5 gps android-location google-play-services

在我的设备(Karbon A 21 手机)中,Google Play Services未安装时,我的location对象是nullGoogle Play Services安装时,它返回正确的位置。

我知道我可以检查是否Google Play Services使用 - 安装GooglePlayServicesUtil.isGooglePlayServicesAvailable(this)。但是我可以Google Play Services实用地安装还是应该要求用户手动安装它?

为什么其他应用即使Google Play Services没有安装也显示位置?我在我的代码中做错了什么吗?请参阅下面的代码。

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  Criteria criteria = new Criteria();
  criteria.setCostAllowed(true);
  provider = locationManager.getBestProvider(criteria, true);
  Location location = locationManager.getLastKnownLocation(provider);
Run Code Online (Sandbox Code Playgroud)

Bos*_*rge 0

这就是我所做的。如果 Google Play 服务不可用,它会要求用户安装它,或者安装当前版本(对于 Google 地图)。

final int RQS_GooglePlayServices = 1;

// Check status of Google Play Services
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

// Check Google Play Service Available
try {
    if (status != ConnectionResult.SUCCESS) {
        GooglePlayServicesUtil.getErrorDialog(status, this, RQS_GooglePlayServices).show();
    }
} catch (Exception e) {
    Log.e("Error: GooglePlayServiceUtil: ", "" + e);
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 API19,那么您的清单中将需要以下内容。

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
Run Code Online (Sandbox Code Playgroud)