【问题标题】:Programatically install `Google Play Services` in Android device以编程方式在 Android 设备中安装“Google Play 服务”
【发布时间】:2023-03-20 04:40:01
【问题描述】:

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

我知道我可以使用-GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) 检查Google Play Services 是否已安装。 但是我可以实际安装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);

【问题讨论】:

  • 我在安装Google Play Services时也遇到了同样的问题。

标签: gps google-play-services android-location


【解决方案1】:

这就是我所做的。如果 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);
}

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多