Simple Facebook Login in Android App :
- Create your app in Developers facebook
- Include FacebookSdk Project as lib project in your app or put jar file in libs.
1. AndroidManifest.xml
Include this in AndroidManifest.xml
<activity
android:name="com.facebook.LoginActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
2. In XML layout create a button which when clicked will help us log with Facebook.
3. MainActivity.java
public class MainActivity extends ActionBarActivity {
private static final List<String> PERMISSIONS = Arrays.asList("email",
"user_about_me", "user_location");
Button mLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebookHashKey();
mLogin=(Button)findViewById(R.id.login);
mLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
loginWithFacebook();
}
});
}
void loginWithFacebook(){
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@SuppressWarnings("deprecation")
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
// make request to the /me API
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
// pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
MainActivity.this, PERMISSIONS);
session.requestNewReadPermissions(newPermissionsRequest);
return;
}
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
// callback after Graph API response with
// user object
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user == null) {
Toast.makeText(
MainActivity.this
.getApplicationContext(),
"Facebook Error",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
MainActivity.this
.getApplicationContext(),
user.getName()
+ " Logged in Successfully.",
Toast.LENGTH_LONG).show();
GraphUser abc = user;
Log.e("ID Name", user.getId()+""+user.getFirstName() + ""+
user.getLastName()+"" + user.getUsername() );
}
return;
}
});
}
}
});
}
No comments:
Post a Comment