2016-12-18 88 views
0

我的飛濺活動中的Facebook登錄按鈕正在關閉飛濺活動,而無需進行下一個活動。通過logcat的外觀,onSuccess方法甚至沒有被調用。無論如何,讓它去下一個活動?Facebook登錄按鈕關閉登錄活動

public class SplashActivity extends Activity { 

    private Intent intent; 
    private Handler mHandler; 
    private Runnable mNextRunnable; 
    private final long SPLASH_TIME = 3000; 
    private VideoView videoHolder; 
    private LoginButton loginButton; 
    private CallbackManager callbackManager; 
    private AccessTokenTracker accessTokenTracker; 
    private ImageView iv; 
    private static final String TAG = SplashActivity.class.getSimpleName(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     FacebookSdk.sdkInitialize(getApplicationContext()); 
     setContentView(R.layout.activity_splash); 

//  try { 
//   PackageInfo info = getPackageManager().getPackageInfo(
//     "info.nexrave.nexrave", 
//     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 (Exception e) { 
// 
//  } 

     Log.d(TAG, "Video about to be called"); 
     //Video settings 
     videoHolder = (VideoView) findViewById(R.id.videoView); 
     Uri video = Uri.parse("android.resource://" + getPackageName() + "/" 
       + R.raw.video_footage); 
     videoHolder.setVideoURI(video); 
     videoHolder.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer mp) { 
       mp.setLooping(true); 
      } 
     }); 
     videoHolder.start(); 

     //Facebook settings 
     iv = (ImageView) findViewById(R.id.iv_splash_logo); 
     loginButton = (LoginButton) findViewById(R.id.login_button); 
     // Callback registration 
     callbackManager = CallbackManager.Factory.create(); 
     loginButton.setReadPermissions("public_profile", "email", "user_friends"); 
     Log.d(TAG, "UpdateWithToken Called: registering callback"); 
     loginButton.registerCallback(callbackManager, mCallBack); 
//  AppEventsLogger.activateApp(this); 
     Log.d(TAG, "UpdateWithToken About To Be Called"); 
//  accessTokenTracker = new AccessTokenTracker() { 
//   @Override 
//   protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) { 
//    updateWithToken(newAccessToken); 
//   } 
//  }; 
     updateWithToken(AccessToken.getCurrentAccessToken()); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     callbackManager.onActivityResult(requestCode, resultCode, data); 
    } 


    @Override 
    protected void onPause() { 
     super.onPause(); 
     finish(); 
    } 

    private void makeVisible() { 
     iv.setVisibility(View.VISIBLE); 
     iv.bringToFront(); 
     loginButton.setVisibility(View.VISIBLE); 
     loginButton.bringToFront(); 
    } 

    private void updateWithToken(AccessToken currentAccessToken) { 
     Log.d(TAG, "UpdateWithToken Called"); 
     if (currentAccessToken != null) { 
      new Handler().postDelayed(new Runnable() { 

       @Override 
       public void run() { 
        Log.d(TAG, "UpdateWithToken Called: User logged in"); 
        Intent i = new Intent(SplashActivity.this, FeedActivity.class); 
        startActivity(i); 
       } 
      }, SPLASH_TIME); 

     } else { 
      new Handler().postDelayed(new Runnable() { 

       @Override 
       public void run() { 
        Log.d(TAG, "UpdateWithToken Called: Not logged in"); 
        makeVisible(); 
        Log.d(TAG, "UpdateWithToken Called: finished"); 
       } 
      }, SPLASH_TIME); 
     } 
    } 

    private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      // App code 
      Log.d(TAG, "UpdateWithToken Called: Done"); 
      //Placeholder: Need to check firebase to see if already registered 
      intent = new Intent(SplashActivity.this, EnterPhoneNumber.class); 
      startActivity(intent); 
     } 

     @Override 
     public void onCancel() { 
      // App code 
      Log.d(TAG, "UpdateWithToken Called: cancel"); 
     } 

     @Override 
     public void onError(FacebookException exception) { 
      // App code 
      Log.d(TAG, "UpdateWithToken Called: error"); 
     } 
    }; 
} 
+0

提供您的logcat輸出...除非它很難調試 – rafsanahmad007

+0

@ rafsanahmad007 Logcat輸出顯示我在那裏的每一個日誌,除了在FacebookCallback中的一個人 – Shane

回答

0

的事情,我注意到情侶...

聲明

Profile profile; 
private ProfileTracker mProfileTracker; 

在你的onCreate()

FacebookSdk.sdkInitialize(getApplicationContext()); 
    callbackManager = CallbackManager.Factory.create(); 
    updateWithToken(AccessToken.getCurrentAccessToken()); 
    accessTokenTracker = new AccessTokenTracker() { 
     @Override 
     protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) { 
      updateWithToken(newToken); 
     } 
    }; 
    accessTokenTracker.startTracking(); 

    loginButton = (LoginButton) findViewById(R.id.login_button); 
    loginButton.setReadPermissions(Arrays.asList(
      "public_profile", "email", "user_friends")); 
    loginButton.registerCallback(callbackManager, callback); 

在updateWithToken()方法

private void updateWithToken(AccessToken currentAccessToken) { 

    if (currentAccessToken != null) { 
     //User logged In--do your stuff 
    } else { 
     //user not logged in 
    } 
} 

而且在回調方法

private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() { 

    @Override 
    public void onSuccess(LoginResult loginResult) { 
     try { 
      if (Profile.getCurrentProfile() == null) { 
       mProfileTracker = new ProfileTracker() { 
        @Override 
        protected void onCurrentProfileChanged(Profile profile_old, Profile profile_new) { 
         // profile2 is the new profile 
         profile = profile_new; 
         Log.v("facebook - profile", profile_new.getFirstName()); 
         mProfileTracker.stopTracking(); 
        } 
       }; 
       mProfileTracker.startTracking(); 
      } else { 
       profile = Profile.getCurrentProfile(); 
       Log.v("facebook - profile", profile.getFirstName()); 
      } 

      Intent intent = new Intent(getApplicationContext(), EnterPhoneNumber.class); 
      startActivity(intent); 
     } catch (Exception e) { 
      Log.d("ERROR", e.toString()); 
     } 
    } 

    @Override 
    public void onCancel() { 

    } 

    @Override 
    public void onError(FacebookException e) { 
     Log.d("FACEBOOK ERRROR", e.toString()); 
    } 
}; 

還要檢查密鑰散列在開發面板,添加

添加這對清單:

<activity 
     android:name="com.facebook.FacebookActivity" 
     android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" 
     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" /> 
在佈局

另外:

<com.facebook.login.widget.LoginButton xmlns:fb="http://schemas.android.com/apk/res-auto" 
       android:id="@+id/login_button" 
       android:layout_width="match_parent" 
       android:layout_height="50dp" 
       android:layout_centerHorizontal="true" 
       android:layout_marginBottom="10dp" 
       android:layout_marginLeft="@dimen/activity_left_margin" 
       android:layout_marginRight="@dimen/activity_right_margin" 
       android:background="@drawable/custom_facebook" 
       android:drawableLeft="@drawable/fb" 
       android:drawablePadding="15dp" 
       android:gravity="center" 
       android:padding="10dp" 
       android:singleLine="true" 
       android:text="Login with Facebook" 
       android:textColor="@color/color_white" 
       android:textSize="18sp" 
       android:visibility="visible" /> 
+0

即使我開始跟蹤,accesstoken仍然爲空。從不調用oncallback方法 – Shane

+0

哈希鍵被正確添加,我直接從android monitor/logcat中複製它。 – Shane

+0

@Shane看到編輯的答案,如果沒有工作...嘗試卸載應用程序並重新安裝它..我發現,由LoginManager返回的訪問令牌將保存在共享首選項 – rafsanahmad007