Tải bản đầy đủ - 0 (trang)
CHƯƠNG II: TẠO PROJECT VÀ THỰC HIỆN ỨNG DỤNG

CHƯƠNG II: TẠO PROJECT VÀ THỰC HIỆN ỨNG DỤNG

Tải bản đầy đủ - 0trang

*) Code:

Layout:




android:layout_width="match_parent"

android:layout_height="match_parent"



android:padding="16dp">




android:id="@+id/info"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_alignParentTop="true"

android:textSize="18sp" />




android:id="@+id/profile_img"

android:layout_width="200dp"

android:layout_height="200dp"

android:layout_centerHorizontal="true"

android:layout_below="@id/info"/>




android:id="@+id/login_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true" />







Code chính cho class:

package co.coderiver.facebooklogin_sample;



import android.content.Intent;

import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.widget.ImageView;

import android.widget.TextView;



import com.bumptech.glide.Glide;

import com.facebook.CallbackManager;

import com.facebook.FacebookCallback;

import com.facebook.FacebookException;

import com.facebook.FacebookSdk;

import com.facebook.Profile;

import com.facebook.login.LoginResult;

import com.facebook.login.widget.LoginButton;



public class MainActivity extends ActionBarActivity {



private CallbackManager callbackManager;

private TextView info;



private ImageView profileImgView;

private LoginButton loginButton;



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);



FacebookSdk.sdkInitialize(getApplicationContext());

callbackManager = CallbackManager.Factory.create();



setContentView(R.layout.activity_main);



info = (TextView) findViewById(R.id.info);

profileImgView = (ImageView) findViewById(R.id.profile_img);

loginButton = (LoginButton) findViewById(R.id.login_button);



/*loginButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {

doOpenChildActivity();

}

});*/



loginButton.registerCallback(callbackManager, new

FacebookCallback() {



@Override

public void onSuccess(LoginResult loginResult) {

Profile profile = Profile.getCurrentProfile();

info.setText(message(profile));



String userId = loginResult.getAccessToken().getUserId();

String profileImgUrl = "https://graph.facebook.com/" + userId + "/picture?

type=large";



Glide.with(MainActivity.this)

.load(profileImgUrl)

.into(profileImgView);



}



@Override

public void onCancel() {

info.setText("Login attempt cancelled.");

}



@Override

public void onError(FacebookException e) {

e.printStackTrace();



info.setText("Login attempt failed.");

}

});

}



@Override

public void onResume() {

super.onResume();

Profile profile = Profile.getCurrentProfile();

info.setText(message(profile));

}



@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

callbackManager.onActivityResult(requestCode, resultCode, data);

}



/* private void doOpenChildActivity() {

Intent myIntent=new Intent(this, MainActivity2.class);

startActivity(myIntent);

}*/



private String message(Profile profile) {



StringBuilder stringBuffer = new StringBuilder();

if (profile != null) {

stringBuffer.append("Welcome ").append(profile.getName());

}

return stringBuffer.toString();

}



}



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

CHƯƠNG II: TẠO PROJECT VÀ THỰC HIỆN ỨNG DỤNG

Tải bản đầy đủ ngay(0 tr)

×