How to get the User detail through facebook in Android Studio..
Creating Facebook App.
Go to https://developers.facebook.com/.
First we need to add Volley Library to our project.
compile 'com.facebook.android:facebook-android-sdk:4.0.0'
Creating New Project.
Open android studio and create a new project.
File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.
Create Xml file in project.
Open => app => res => layout - activity_main.xml.
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="24dp"
android:background="@drawable/bt_facebook"
android:padding="12dp"
android:layout_marginTop="12dp"
/>
Create the Java file in project.
Open app => main => src = MainActivity.java
private CallbackManager callbackManager;
LoginButton login_button;
String email,name,first_name,last_name;
//*******savedInstanceState******//
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
//***put the facebookSDK and callbackManager Object in middle saveInstanceState and setContentView*****//
login_button=(LoginButton) findViewById(R.id.login_button);
login_button.setReadPermissions(Arrays.asList("public_profile","email"));
login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
{
@Override
public void onSuccess(LoginResult loginResult)
{
GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
@Override
public void onCompleted(JSONObject object, GraphResponse response)
{
sendData(object);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,first_name,last_name,email");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
@Override
public void onCancel()
{
}
@Override
public void onError(FacebookException exception)
{
}
});
}
private void sendData(JSONObject object) {
try {
email = object.getString("email");
name = object.getString("name");
first_name = object.optString("first_name");
last_name = object.optString("last_name");
} catch (JSONException e) {
e.printStackTrace();
}
Intent main = new Intent(MainActivity.this, YourActivity.class);
main.putExtra("email", email);
main.putExtra("name", name);
startActivity(main);
}
@Override
protected void onRestart() {
LoginManager.getInstance().logOut();
super.onRestart();
}
@Override
protected void onResume() {
LoginManager.getInstance().logOut();
super.onResume();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
callbackManager.onActivityResult(requestCode, resultCode, intent);
}
Add Internet permission in your manifest.
<uses-permission android:name="android.permission.INTERNET" />
Creating Facebook App.
Go to https://developers.facebook.com/.
First we need to add Volley Library to our project.
compile 'com.facebook.android:facebook-android-sdk:4.0.0'
Creating New Project.
Open android studio and create a new project.
File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.
Create Xml file in project.
Open => app => res => layout - activity_main.xml.
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="24dp"
android:background="@drawable/bt_facebook"
android:padding="12dp"
android:layout_marginTop="12dp"
/>
Create the Java file in project.
Open app => main => src = MainActivity.java
private CallbackManager callbackManager;
LoginButton login_button;
String email,name,first_name,last_name;
//*******savedInstanceState******//
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
//***put the facebookSDK and callbackManager Object in middle saveInstanceState and setContentView*****//
login_button=(LoginButton) findViewById(R.id.login_button);
login_button.setReadPermissions(Arrays.asList("public_profile","email"));
login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
{
@Override
public void onSuccess(LoginResult loginResult)
{
GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
@Override
public void onCompleted(JSONObject object, GraphResponse response)
{
sendData(object);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,first_name,last_name,email");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
@Override
public void onCancel()
{
}
@Override
public void onError(FacebookException exception)
{
}
});
}
private void sendData(JSONObject object) {
try {
email = object.getString("email");
name = object.getString("name");
first_name = object.optString("first_name");
last_name = object.optString("last_name");
} catch (JSONException e) {
e.printStackTrace();
}
Intent main = new Intent(MainActivity.this, YourActivity.class);
main.putExtra("email", email);
main.putExtra("name", name);
startActivity(main);
}
@Override
protected void onRestart() {
LoginManager.getInstance().logOut();
super.onRestart();
}
@Override
protected void onResume() {
LoginManager.getInstance().logOut();
super.onResume();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
callbackManager.onActivityResult(requestCode, resultCode, intent);
}
Add Internet permission in your manifest.
<uses-permission android:name="android.permission.INTERNET" />
No comments:
Post a Comment