简体   繁体   中英

checking the availability of Google Play services on a device

i am trying to check the availability of Google Play services on a device. but the app is crashing on start up. the app runs without the servicesOK() method, and i have imported the google play services into my project.. i'm not sure, or understand, why the app is crashing.

here is my MainActivity.java (the only code in the app so far):

package com.example.gmapsapp;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

private static final int GPS_EERRORDIALOG_REQUEST = 9001;
GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(servicesOK()){
              Toast.makeText(this,  "message", Toast.LENGTH_SHORT).show();
    }

    setContentView(R.layout.activity_main);
}

public boolean servicesOK(){

    int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if(isAvailable == ConnectionResult.SUCCESS){
        return true;
    }
    else if(GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
        Dialog dialog = 
                GooglePlayServicesUtil.getErrorDialog(isAvailable, 
                         this, GPS_EERRORDIALOG_REQUEST);
        dialog.show();
    }
    else {
        Toast.makeText(this,  "Cannot Connect to to Google Play services", Toast.LENGTH_SHORT).show();
    }
    return false;
}

}

Fixed. i was missing the following code from the manifest file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM