Friday, December 28, 2012

Check if correct Google Play Service available for Google Maps Android API v2

According to Google Play Services Document, apps using Google Play Service API should Ensuring Devices Have the Google Play services APK. So we have to check isGooglePlayServicesAvailable() and call getErrorDialog() if not SUCCESS.

Actually in my trial experience to uninstall Google Play Service before running. The app will ask to install Google Play Service automatically, even I haven't check isGooglePlayServicesAvailable(). So I can't clarify if my code work correctly or not!

update@2013-07-16: show() must be called. ~ thanks Morrox comment.
 
 GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices).show();
 


Check if correct Google Play Service available for Google Maps Android API v2


Modify the java code from the last post "Include open source software license information/Legal Notices in your app using Google Maps Android API v2", to check isGooglePlayServicesAvailable() in onResume().

package com.example.androidmapsv2;

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

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {
 
 final int RQS_GooglePlayServices = 1;

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

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
     case R.id.menu_legalnotices:
      String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
        getApplicationContext());
      AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
      LicenseDialog.setTitle("Legal Notices");
      LicenseDialog.setMessage(LicenseInfo);
      LicenseDialog.show();
         return true;
     }
  return super.onOptionsItemSelected(item);
 }

 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();

  int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
  
  if (resultCode == ConnectionResult.SUCCESS){
   Toast.makeText(getApplicationContext(), 
     "isGooglePlayServicesAvailable SUCCESS", 
     Toast.LENGTH_LONG).show();
  }else{
   GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
  }
  
 }

}


download filesDownload the files.


The series:
A simple example using Google Maps Android API v2, step by step.

5 comments:

Morrox said...

It must be called show:

GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices).show();

Andr.oid Eric said...

Thanks Morrox:)

Anonymous said...

This was great! Thank you very much :D

Anonymous said...

That was great. Thank you. However the application crashes every time. I downloaded you attached codes. Is there something I'm doing wrongly.

Luco said...

hello,

you must initialise the int RQS_GooglePlayServices like so:

int RQS_GooglePlayServices = 0;


cheers,
Luco