Android Checking the availability of Google Play Services for Google Map Android API V2

January 24, 2013
By

We know that, Google Map Android API V2 requires Google Play Services to work and so it is always a best practice to check whether the Google Play Services are installed in the device on starting up the Google Map application.

In this article, we will create an application which checks whether Google Play Services are available in the hosting device on starting up the application. If the service is not available, a dialog window appears to prompt user to install Google Play Services in the device.

This application is developed in Eclipse(4.2.1) with ADT plugin(21.0.0) and Android SDK(21.0.0).


1. Download and configure Google Play Services Library in Eclipse

Please follow the given below link to setup Google Play Service library in Eclipse.

http://developer.android.com/google/play-services/setup.html


2. Create a new Android application project

Create a new Android application project

Figure 1 : Create a new Android application project


3. Configure the project

Configure the project

Figure 2 : Configure the project


4. Design application launcher icon

Design application launcher icon

Figure 3 : Design application launcher icon


5. Create a blank activity

Create a blank activity

Figure 4 : Create a blank activity


6. Enter MainActivity details

Enter Main Activity Details

Figure 5 : Enter Main Activity Details


7. Add Google Play Services Library to this project

Add Google Play Services library to this project

Figure 6 : Add Google Play Services library to this project


8. Update the layout file res/layout/activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>


9. Update the file src/in/wptrafficanalyzer/checkgoogleplayservices/MainActivity.java


package in.wptrafficanalyzer.checkgoogleplayservices;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

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

public class MainActivity extends Activity {

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

        // Getting reference to TextView to show the status
        TextView tvStatus = (TextView)findViewById(R.id.tv_status);

        // Getting status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if(status==ConnectionResult.SUCCESS)
            tvStatus.setText(&quot;Google Play Services are available&quot;);
        else{
            tvStatus.setText(&quot;Google Play Services are not available&quot;);
            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();
        }
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}



10. Executing the application

Executing the application in an Android Emulator

Figure 7 : Executing the application in an Android Emulator

Executing the application in a real device

Figure 8 : Executing the application in a real device


11. Download the source code


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , ,

3 Responses to Android Checking the availability of Google Play Services for Google Map Android API V2

  1. K on May 17, 2013 at 8:27 pm

    hi. i’ve made an app using maps api v2 and when i start that app, it crashes if the gps is off. can you show how to check for that and enable it so the app doesn’t crash?

  2. Fabio on August 2, 2013 at 12:17 am

    Hi, I use your code and works perfeclty for me.

  3. prasanth on July 8, 2015 at 5:34 pm

    Its checking but not install the google play services

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner