Tuesday, 30 December 2014

Programmatically check google play services exist or not in android device

In this post, we will implement a very basic (but robust) Android application that authenticates a user with Google services.

 1.  Add Google PlayServices library

2. Activity.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">

    <TextView android:id="@+id/googleplayServies"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

3. String.Xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">GooglePlayServiceUpdate</string>
    <string name="googleplayServiesText">Google Play Services Updated</string>
</resources>

4. AndroidManifest.Xml:

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

5. MainActivity.Java:
package com.example.checkgoogleplayserviceupdate;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
private TextView googleplayServies;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
googleplayServies = (TextView) findViewById(R.id.googleplayServies);
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.this);
if (status != ConnectionResult.SUCCESS) {
updateGoogleplay();
}
 else
googleplayServies.setText(R.string.googleplayServiesText);
}

public void updateGoogleplay() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
// set title
alertDialogBuilder.setTitle("Update Google Play Services");
// set dialog message
alertDialogBuilder
.setMessage("This Application Want To Update You Google Play Services App")
.setCancelable(false)
.setPositiveButton("Update",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
callMarketPlace();
finish();
 }
});
alertDialogBuilder.show();
}
public void callMarketPlace() {
try {
startActivityForResult(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id="+ "com.google.android.gms")), 1);
}
 catch (android.content.ActivityNotFoundException anfe) {
startActivityForResult(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id="+ "com.google.android.gms")), 1);
}
}
}

No comments:

Post a Comment