5

In My Device( Karbon A 21 Mobile Phone) , When Google Play Services are not installed, my location object is null and while Google Play Services installed, it returns correct location.

I know that I can check if Google Play Services are installed using-GooglePlayServicesUtil.isGooglePlayServicesAvailable(this). But then Can I install Google Play Services pragmatically or should I ask user to install it manually?

Why other App shows location even if Google Play Services are not installed? Am I doing something wrong in my code. See code below.

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

1 Answers1

0

Here is what I do. It asks the user to install Google Play Services if it is not available, or the current version (for Google Maps).

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

If you are using API19, then you will need the following in your manifest.

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
BostonGeorge
  • 3,092
  • 1
  • 17
  • 12