Search code examples
androidgoogle-plusandroid-sharing

Share in google plus from android application


I have to share some text and a link from android application for that I have wrote code as follows

        PlusShare.Builder shareBuilder = new PlusShare.Builder(this);
        shareBuilder.setType("text/plain");
        shareBuilder.setText("text to be shared");
        shareBuilder.setContentUrl(Uri.parse("link_to_share"));
        Intent shareIntent = shareBuilder.getIntent();
        startActivityForResult(shareIntent, SHARE_GOOGLE_PLUS_REQUEST_CODE);

this code works fine and I can share to google plus, and am getting a call back in onActivityResult(). what my issue is if the user not installed google plus, the app stop responds and exits. how do I fix this


Solution

  • Try the following code:

    // Google client to interact with Google API
    private GoogleApiClient mGoogleApiClient;
    mGoogleApiClient = new GoogleApiClient.Builder(ThirtyArticleDetail.this)
        .addConnectionCallbacks(ThirtyArticleDetail.this)
        .addOnConnectionFailedListener(ThirtyArticleDetail.this).addApi(Plus.API)
        .addScope(Plus.SCOPE_PLUS_LOGIN).build();
    
    btnsharegplus.setOnClickListener(new View.OnClickListener() {
    
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
    
                    if (!mGoogleApiClient.isConnected()) {
                        // Set sharing so that the share is started in onConnected.
                        mSignInClicked= true;
    
                        if (!mGoogleApiClient.isConnecting()) {
                            mGoogleApiClient.connect();
                        }
                    } else {
                        Intent shareIntent = new PlusShare.Builder(ThirtyArticleDetail.this)
                        .setType("text/plain")
                        .setText("Welcome to the Google+ platform.")
                        .setContentUrl(Uri.parse(articleurl))
                        //.setContentUrl(Uri.parse("https://developers.google.com/+/"))
                        .getIntent();
    
                        startActivityForResult(shareIntent, 0);
                    }
                }
            });
    
    
    
    
    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub
        if (!result.hasResolution()) {
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                    0).show();
            return;
        }
    
        if (!mIntentInProgress) {
            // Store the ConnectionResult for later usage
            mConnectionResult = result;
    
            if (mSignInClicked) {
                // The user has already clicked 'sign-in' so we attempt to
                // resolve all
                // errors until the user is signed in, or they cancel.
                resolveSignInError();
    
    
            }
        }
    }
    
    
    
    @Override
    public void onConnected(Bundle arg0) {
        // TODO Auto-generated method stub
        mSignInClicked = false;
        // Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
        Intent shareIntent = new PlusShare.Builder(this)
        .setType("text/plain")
        .setText("Welcome to the Google+ platform.")
        .setContentUrl(Uri.parse(articleurl))
        //.setContentUrl(Uri.parse("https://developers.google.com/+/"))
        .getIntent();
    
        startActivityForResult(shareIntent, 0);
    
    
    }
    
    
    
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        //mGoogleApiClient.connect();
    }
    
    
    
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }
    
    
    
    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub
        if(!(mGoogleApiClient.isConnected()))   
            mGoogleApiClient.connect();
    }
    /**
     * Sign-in into google
     * */
    /*private void signInWithGplus() {
        if (!mGoogleApiClient.isConnecting()) {
            mSignInClicked = true;
            resolveSignInError();
        }
    }
     */
    /**
     * Method to resolve any signin errors
     * */
    private void resolveSignInError() {
        try {
            if (mConnectionResult.hasResolution()) {
    
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
            }
        } catch (SendIntentException e) {
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
        catch (Exception e) {
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    
    }
    

    First of all please go through the follwing links and understand what GoogleApiClient actually is:

    https://developers.google.com/android/guides/api-client

    http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/

    https://developers.google.com/+/mobile/android/share/

    EDIT

    Dont forget to take the following permissions in manifest.xml:

     <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <uses-permission android:name="android.permission.USE_CREDENTIALS" />