Sunday, October 16, 2016

Social Integration (FaceBook) in your App- Part II

Hi..... Dear All ....  Hope you know how to integrate FaceBook in your Android App. if not please visit
http://androidclue4u.blogspot.in/2016/08/social-integration-facebook-in-your-app.html

know in this post i am carrying the same example which is done on mentioned link.
Know i am trying to make it more realistic .... Like
@ How you can retrieve Profile pic from FB account
@ How you can get user information   Name   , First Name , Last Name from FB a/c

For this i have added two new classes like ProfileTracker and AccessTokenTracker 
Another important component is Android Query (aquery).
I had change activity_main layout also .... now the output will be like
A ... Output
a.


b.

c.

B. Layout file is as

<?xml version="1.0" encoding="utf-8"?>

<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"

      tools:context="com.exam.ravi.socialintegration.MainActivity" >

    <com.facebook.login.widget.LoginButton

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/loginFB"

        android:layout_alignParentBottom="true"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:layout_marginBottom="137dp" />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="20dp"

        android:text="New Text"

        android:id="@+id/name"
        android:layout_centerVertical="true"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true"

        android:textAlignment="center" />

    <ImageView        android:layout_width="200dp"

        android:layout_height="200dp"

        android:id="@+id/imageView"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true" />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="20dp"

        android:text="New Text"

        android:id="@+id/fname"

        android:layout_below="@+id/name"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:textAlignment="center" />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="20dp"

        android:text="New Text"

        android:id="@+id/lname"

        android:layout_below="@+id/fname"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:textAlignment="center" />

</RelativeLayout>

C. Java code is as

package com.exam.ravi.socialintegration;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class MainActivity extends AppCompatActivity {
    TextView name,fname,lname;
    LoginButton loginButton;
    CallbackManager callbackManager;
    private ProfileTracker profileTracker;
    private AccessTokenTracker accessTokenTracker;
    AQuery aQuery;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        callbackManager = CallbackManager.Factory.create();
        aQuery = new AQuery(this);
        loginButton = (LoginButton) findViewById(R.id.loginFB);
        loginButton.setReadPermissions("public_profile");
        name = (TextView) findViewById(R.id.name);
        fname = (TextView) findViewById(R.id.fname);
        lname = (TextView) findViewById(R.id.lname);
        loginButton.registerCallback(callbackManager,
                              new FacebookCallback<LoginResult>() {
            @Override

            public void onSuccess(LoginResult loginResult) {

               Profile profile = Profile.getCurrentProfile();
                if(profile==null)
                {
                    profileTracker = new ProfileTracker() {
                        @Override

                        protected void onCurrentProfileChanged(Profile oldProfile, 
                                                    Profile currentProfile) {
                      if(currentProfile!=null) {
                          showProfile(currentProfile);
                          Log.v("Ravi", "Logging Current Profile");
                      }
                      if(oldProfile!=null)
                      {
                          showProfile(oldProfile);
                          Log.v("Ravi", "Logging Old Profile");
                      }
                        }
                    };

                    accessTokenTracker = new AccessTokenTracker() {
                        @Override

                        protected void onCurrentAccessTokenChanged
                         (AccessToken oldAccessToken,AccessToken currentAccessToken) 
                        {
                            Toast.makeText(MainActivity.this,"Token Changed",
                                                      Toast.LENGTH_LONG).show();
                        }
                    };
                    accessTokenTracker.startTracking();
                    profileTracker.startTracking();
                }
                else                {
                    showProfile(profile);
                }
           
            }

            @Override            public void onCancel() {
                Log.v("Ravi", "User Cancelled");
            }

            @Override            public void onError(FacebookException error) {
                Log.v("Ravi", "Something Wrong");
            }
        });
    }
    public  void showProfile(Profile recProfile)
    {
        name.setText("Name is =  " + recProfile.getName());
        fname.setText( " First Name = " + recProfile.getFirstName());
        lname.setText("Last Name = " + recProfile.getLastName());
        String image_string = recProfile.getProfilePictureUri(200,200).toString();
        aQuery.id(R.id.imageView).image(image_string);

    }
    @Override 

    protected void onStop() {
        super.onStop();
        if(profileTracker!=null)
            profileTracker.stopTracking();
        if(accessTokenTracker!=null)
            accessTokenTracker.startTracking();
    }

    @Override 

   protected void onActivityResult( int req, int res, Intent intnt)
        {
            callbackManager.onActivityResult(req,res,intnt);
        }

}
    

No comments:

Post a Comment