Tải bản đầy đủ (.docx) (15 trang)

báo cáo định kỳ CÁC CÔNG CỤ LẬP TRÌNH VÀ ĐĂNG KÍ FACEBOOK API

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (222.29 KB, 15 trang )

Đánh giá của cơ sở thực tập

…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
Đánh giá của giáo viên hướng dẫn




…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………

MỤC LỤC


CHƯƠNG I: CÁC CÔNG CỤ LẬP TRÌNH VÀ ĐĂNG KÍ FACEBOOK API.............................4
1.

Các công cụ lập trình trên Android:......................................................................................4

2.

Các công cụ lập trình Facebook API:...................................................................................4

3.

Đăng kí Facebook API:.........................................................................................................4
a)

Đăng kí App cho đề tài:.....................................................................................................4

CHƯƠNG II: TẠO PROJECT VÀ THỰC HIỆN ỨNG DỤNG.....................................................6
1. Login và lấy Avatar:.................................................................................................................6

CHƯƠNG I: CÁC CÔNG CỤ LẬP TRÌNH VÀ ĐĂNG KÍ
FACEBOOK API.
1. Các công cụ lập trình trên Android:
- Phần mềm Android Studio 1.2.2 : Tạo project và thực hiện ứng dụng trong
android.
- Phần mềm Genymotion và Oracle VM VirtualBox: Tạo máy ảo Android demo ứng
dụng.
- JDK Phiên bản 1.7: Tạo môi trường Java.

- SDK Android: Tạo môi trường Android cho Android Studio.
2. Các công cụ lập trình Facebook API:
- SDK Facebook: Lấy các chức năng của Facebook API cung cấp.
- Phần mềm OpenSSL : Lấy Hash key đăng kí cho App Android.
3. Đăng kí Facebook API:
a) Đăng kí App cho đề tài:

+ Truy cập vào trang: />+ Vào phần “My APPs” và chọn “Add a new app”, xuất hiện một bản thông báo
ta chọn Phần Android.
+ Đặt tên cho đề tài.
+ Điền đầy đủ thông tin cho đề tài.
+ Điền Hash key cho đề tài: Có 2 cách lấy Hash key, có thể dung code hoặc dung
phần mềm:


*) Code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add code to print out the key hash
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.facebook.samples.hellofacebook",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(),
Base64.DEFAULT));
}

} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
*) Dùng phần mềm:
Vào theo địa chỉ: C:\Program Files\Java\jdk1.7.0_79\bin
Giữ phím “Shift” Và nhấn chuột phải chọn “Open command Window here”
Sauk hi xuất hiện một bảng CMD ta nhập đoạn code sau:
keytool -exportcert -alias androiddebugkey -keystore "C:\Users\Hoang
Duc\.android\debug.keystore" | "C:\OpenSSL\bin\openssl" sha1 -binary |
"C:\OpenSSL\bin\openssl" base64
ấn Enter và xuất hiện 1 đoạn mã đó là Hash key.

CHƯƠNG II: TẠO PROJECT VÀ THỰC HIỆN ỨNG DỤNG
1. Login và lấy Avatar:
*) Giao diện:



*) Code:
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=" />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" />

</RelativeLayout>


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<LoginResult>() {


@Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
info.setText(message(profile));

String userId = loginResult.getAccessToken().getUserId();
String profileImgUrl = " + 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();
}

}



×