Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android + Firebase + FB 建國科技大學 資管系 饒瑞佶 2017/2 v1.

Similar presentations


Presentation on theme: "Android + Firebase + FB 建國科技大學 資管系 饒瑞佶 2017/2 v1."— Presentation transcript:

1 Android + Firebase + FB 建國科技大學 資管系 饒瑞佶 2017/2 v1

2 Android加入Firebase

3 Android加入Firebase

4 輸入Android專案packagename與SHA1

5 取得Android專案SHA1

6 下載google-services.json

7 修改build.gradle 將google-services.json加入 Android專案

8 Android加入Firebase後 再加入FB驗證

9 FB建立應用程式

10 選擇Facebook登入 到這就有應用程式ID與密鑰

11 輸入Firebase導向URL

12 從Firebase取得導向URL

13 FB中加入Android

14 選擇Android

15 依據建議修改Android專案的buid.gradle

16 依據建議修正Android專案

17 填入Anadroid套件名稱與對應Activity

18 整合 Android+Firebase+FB auth

19 加入與確認必要的dependences

20 packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/LICENSE-FIREBASE.txt' } repositories { mavenCentral() } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.1.1' testCompile 'junit:junit:4.12' compile 'com.firebase:firebase-client-android:2.5.2+' compile 'com.facebook.android:facebook-android-sdk:[4,5)' compile 'com.google.firebase:firebase-auth:10.2.0' } apply plugin: 'com.google.gms.google-services'

21 修改AndroidManifest.xml

22 修改AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET" /> <meta-data android:name="com.facebook.sdk.ApplicationId" /> <activity android:name="com.facebook.FacebookActivity" />

23 修改strings.xml <resources>
<string name="app_name">FBAuth</string> <string name="facebook_app_id"> </string> </resources>

24 建立UI (I) <LinearLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="edu.ctu.rcjao.fbauth.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="signed_out" /> tools:text="Firebase User ID: abc" />

25 建立UI (II) <com.facebook.login.widget.LoginButton
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <Button android:layout_centerInParent="true" android:text="sign_out" android:visibility="gone" /> </LinearLayout>

26 程式 public class MainActivity extends AppCompatActivity implements
View.OnClickListener { private static final String TAG = "AAA"; private TextView mStatusTextView; private TextView mDetailTextView; private FirebaseAuth mAuth; private FirebaseAuth.AuthStateListener mAuthListener; private CallbackManager mCallbackManager; public ProgressDialog mProgressDialog;

27 onCreate事件(I) @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Firebase.setAndroidContext(this); FacebookSdk.sdkInitialize(this); if (BuildConfig.DEBUG) { FacebookSdk.setIsDebugEnabled(true); FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS); } setContentView(R.layout.activity_main); mStatusTextView = (TextView) findViewById(R.id.status); mDetailTextView = (TextView) findViewById(R.id.detail); findViewById(R.id.button_facebook_signout).setOnClickListener(this); // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance(); mAuthListener = new FirebaseAuth.AuthStateListener() { public void FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null) { // User is signed in Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); } else { // User is signed out Log.d(TAG, "onAuthStateChanged:signed_out"); updateUI(user); }; onCreate事件(I)

28 // Initialize Facebook Login button
mCallbackManager = CallbackManager.Factory.create(); LoginButton loginButton = (LoginButton) findViewById(R.id.button_facebook_login); loginButton.setReadPermissions(" ", "public_profile"); loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { Log.d(TAG, "facebook:onSuccess:" + loginResult); handleFacebookAccessToken(loginResult.getAccessToken()); } public void onCancel() { Log.d(TAG, "facebook:onCancel"); updateUI(null); public void onError(FacebookException error) { Log.d(TAG, "facebook:onError", error); });

29 @Override public void onStart() { super.onStart(); mAuth.addAuthStateListener(mAuthListener); } public void onStop() { super.onStop(); hideProgressDialog(); if (mAuthListener != null) { mAuth.removeAuthStateListener(mAuthListener); protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Pass the activity result back to the Facebook SDK mCallbackManager.onActivityResult(requestCode, resultCode, data);

30 private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token); showProgressDialog(); AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken()); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void Task<AuthResult> task) { Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Log.d(TAG, "signInWithCredential", task.getException()); Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } hideProgressDialog(); });

31 public void signOut() {
mAuth.signOut(); LoginManager.getInstance().logOut(); updateUI(null); } private void updateUI(FirebaseUser user) { hideProgressDialog(); if (user != null) { mStatusTextView.setText("facebook user:" + user.getDisplayName()); mDetailTextView.setText("firebase user" + user.getUid()); findViewById(R.id.button_facebook_login).setVisibility(View.GONE); findViewById(R.id.button_facebook_signout).setVisibility(View.VISIBLE); } else { mStatusTextView.setText("signed_out"); mDetailTextView.setText(null); findViewById(R.id.button_facebook_login).setVisibility(View.VISIBLE); findViewById(R.id.button_facebook_signout).setVisibility(View.GONE);

32 @Override public void onClick(View v) { int i = v.getId(); if (i == R.id.button_facebook_signout) { signOut(); } public void showProgressDialog() { if (mProgressDialog == null) { mProgressDialog = new ProgressDialog(this); mProgressDialog.setMessage("loading"); mProgressDialog.setIndeterminate(true); mProgressDialog.show(); public void hideProgressDialog() { if (mProgressDialog != null && mProgressDialog.isShowing()) { mProgressDialog.dismiss();

33 執行驗證 注意,FB的帳號是否已經在之前有在Firebase註冊過 可能會導致任整與登入驗證Firebase失敗

34 最後結果

35

36


Download ppt "Android + Firebase + FB 建國科技大學 資管系 饒瑞佶 2017/2 v1."

Similar presentations


Ads by Google