Sunday 5 May 2013

Google Plus Integration in Android


Before you can start integrating Google+ features in your own app, you must create a Google APIs Console project and initialize the PlusClient within your app.

The Google+ platform for Android has the following requirements:
  • A physical device to use for developing and testing because Google Play services cannot be installed on an emulator.
  • The latest version of the Android SDK, including the SDK Tools component. The SDK is available from the Android SDK Manager.
  • Your project to compile against Android 2.2 (Froyo) or higher.
  • Eclipse configured to use Java 1.6
  • The Google Play services SDK:
    1. Launch Eclipse and select Window > Android SDK Manager or run android from the command line.
    2. Scroll to the bottom of the package list and select Extras > Google Play services. The package is downloaded to your computer and installed in your SDK environment at <android-sdk-folder>/extras/google/google_play_services.

Step 1: Enable the Google+ API

To authenticate and communicate with the Google+ APIs, you must first register your digitally signed .apk file's public certificate in the Google APIs Console:

Step 2: Add Google play service library project in your project

Location of library project is here... <android-sdk-folder>/extras/google/google_play_services/libproject

Step 3: How do I check to see if the Google+ app is installed on the device?

int errorCode = GooglePlusUtil.checkGooglePlusApp(this);

if (errorCode != GooglePlusUtil.SUCCESS) {

GooglePlusUtil.getErrorDialog(errorCode, this, 0).show();

} 

Step 4: Initialize the PlusClient

The PlusClient object is used to communicate with the Google+ service and becomes functional after an asynchronous connection has been established with the service. Because the client makes a connection to a service, you want to make sure the PlusClient.disconnect method is called when appropriate to ensure robustness.

Your activity will listen for when the connection has established or failed by implementing the ConnectionCallbacks and OnConnectionFailedListener interfaces.

You have to create PlusClient object in Activity's onCreate() method.

mPlusClient = new PlusClient(this, this, this, Scopes.PLUS_PROFILE);

You can handle google plus sign in & sign out in your app like this.

if (v.getId() == R.id.sign_in_button) {

            if (!mPlusClient.isConnected()
                    && btnSignIn.getText().equals(
                            getString(R.string.btn_signin))) {

                mPlusClient.connect();

            } else if (mPlusClient.isConnected()
                    && btnSignIn.getText().equals(
                            getString(R.string.btn_signout))) {
                {
                    mPlusClient.clearDefaultAccount();
                    mPlusClient.disconnect();
                    btnSignIn.setText(getString(R.string.btn_signin));
                    textUserName.setText("");
                    txtlogin.setVisibility(View.GONE);

                }
            }

        }
When the PlusClient object is unable to establish a connection, your implementation has an opportunity to recover inside your implementation of onConnectionFailed, where you are passed a connection status that can be used to resolve any connection failures.
@Override
    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub
        if (result.hasResolution()) {
            // The user clicked the sign-in button already. Start to resolve
            // connection errors. Wait until onConnected() to dismiss the
            // connection dialog.
            try {
                result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                mPlusClient.disconnect();
                mPlusClient.connect();
            }
        }
    }

    @Override
    public void onConnected() {
        // TODO Auto-generated method stub
        String accountName = mPlusClient.getAccountName();
        Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG)
                .show();
        btnSignIn.setText(getString(R.string.btn_signout));
        textUserName.setText(accountName);
        txtlogin.setVisibility(View.VISIBLE);

    }

Because the resolution for the connection failure was started with startActivityForResult and the code REQUEST_CODE_RESOLVE_ERR, we can capture the result inside Activity.onActivityResult. 

@Override
    protected void onActivityResult(int requestCode, int responseCode,
            Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, responseCode, data);
        if (requestCode == REQUEST_CODE_RESOLVE_ERR
                && responseCode == RESULT_OK) {
            mPlusClient.disconnect();
            mPlusClient.connect();
        }

    }

 

Features:

Sharing to Google+ from your Android app

The Share dialog provides a means for users to share rich content from your app into the Google+ stream, including text, photos, URL attachments and location. In addition, your app can use two advanced sharing options: interactive posts and deep linking.

you can share on google plus like this

if (v.getId() == R.id.share_button) {

            if (mPlusClient.isConnected()) {
                Intent shareIntent = PlusShare.Builder
                        .from(this)
                        .setText(
                                "Check out: http://example.com/cheesecake/lemon")
                        .setType("text/plain")
                        .setContent(
                                Uri.parse("http://example.com/cheesecake/lemon"))
                        .getIntent();
                startActivity(shareIntent);
            } else {
                Toast.makeText(this, "Please Sign-in with google Account", Toast.LENGTH_LONG)
                .show();
            }
}

 

 Getting people and profile information

After you have signed in a user with Google, you can access the user's age range, language, public profile information, and people that they have circled.

Please find attached demo for Google plus integration.

Demo Example:

You can download source code from here. Source Code Download


Refrences:

https://developers.google.com/+/mobile/android/getting-started#step_1_enable_the_google_api

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