This post is about "How to check google play services exist or not". If some devices don't have Google Play Services then how to call direct google play service update from "Google Play Market".
Hope it helps you!!!!
Happy Coding....!!!!!:)
Sample Screen Shot -
Step 1- You have to import google play service library in to your project explorer.
Go to - File - > Import -> Existing Android Code Into Workspace
Then Click Next
Browse path for your "Android SDK Folder".
My Path is - "D:\shweta\android-sdk-windows\android-sdk-windows\extras\google\google_play_services\libproject"
Important - Then "Check Copy Project to workspace"
Click Finish.
Now you successfully import Google Play Library in to your project explorer.
Step -2 You have to add Google Play Library in to your project .
Go to - Right Click on your project -> Properties -> Android - > Drag Window( Add Library) -> Select "google-play-services-lib" -> Apply -> Ok
Now you successfully import Google Play Library in to your project.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.googleplayservicescheckdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.googleplayservicescheckdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application> </manifest>
activity_main.xml
<LinearLayout 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:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dip" android:layout_marginTop="15dip" android:text="Now your device have google play services" android:textStyle="bold" /> </LinearLayout>
MainActivity.java
package com.example.googleplayservicescheckdemo; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import android.support.v7.app.ActionBarActivity; import android.annotation.TargetApi; 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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int status = GooglePlayServicesUtil .isGooglePlayServicesAvailable(getApplicationContext()); if (status == ConnectionResult.SUCCESS) { // Success! Do what you want setContentView(R.layout.activity_main); } else { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( MainActivity.this); // set title alertDialogBuilder.setTitle("Alert!!1"); // 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(); } }) .setNegativeButton("Cancle", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // if this button is clicked, just close // the dialog box and do nothing finish(); dialog.cancel(); } }); // create alert dialog // AlertDialog alertDialog = alertDialogBuilder.create(); // show it 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); } } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override protected void onActivityResult(int arg0, int arg1, Intent arg2) { // TODO Auto-generated method stub super.onActivityResult(arg0, arg1, arg2); if (arg0 == 1) { if (Build.VERSION.SDK_INT >= 11) { recreate(); } else { Intent intent = getIntent(); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); finish(); overridePendingTransition(0, 0); startActivity(intent); overridePendingTransition(0, 0); } } } }
Hope it helps you!!!!
Happy Coding....!!!!!:)
Hi Shweta. Nice Information covered. One suggestion i would like to give is checking of Google Play Services in onResume() instead of onCreate(). By doing this ur Activity will check everytime it comes in forground if Google Play Services are installed or not. This will avoid other consequences.
ReplyDelete