コード例 #1
0
 private void clearFbData() {
   // user has logged out
   fbShareButton.setEnabled(false);
   SharedPreferences.Editor editor = sp.edit();
   editor.putString("fb_id", "");
   editor.putString("fb_name", "");
   editor.commit();
   profilePictureView.setProfileId(sp.getString("fb_id", ""));
   tv_name.setText(sp.getString("fb_name", ""));
 }
コード例 #2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initFacebook();
    setContentView(R.layout.activity_main);

    sp = this.getSharedPreferences("Accounts", Context.MODE_PRIVATE);
    tv_name = (TextView) findViewById(R.id.tv_name);
    profilePictureView = (ProfilePictureView) findViewById(R.id.proflie_picture);
    profilePictureView.setCropped(true);
    profilePictureView.setPresetSize(ProfilePictureView.NORMAL);

    // setting profile from storage;
    String id = sp.getString("fb_id", "");
    String name = sp.getString("fb_name", "");
    profilePictureView.setProfileId(id);
    tv_name.setText(name);

    fbLoginButton = (LoginButton) findViewById(R.id.fb_login_button);
    fbShareButton = (ShareButton) findViewById(R.id.fb_share_button);
    if (id != "") {
      fbShareButton.setEnabled(true);
    }

    fbShareButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (ShareDialog.canShow(ShareLinkContent.class)) {
              ShareLinkContent linkContent =
                  new ShareLinkContent.Builder()
                      .setContentTitle("I am the king")
                      .setContentDescription("No doubt")
                      .setImageUrl(
                          Uri.parse(
                              "http://vignette2.wikia.nocookie.net/genealogy/images/2/28/King_Henry_I_from_NPG.jpg/revision/latest?cb=20090610170605"))
                      .setContentUrl(Uri.parse("http://stackoverflow.com/"))
                      .build();

              // Mode.FEED:Shared link with this account
              // Mode.NATIVE:Shared link with the account that you have logged in the facebook app
              // So, if your app ask for login, you use Mode.FEED to share link
              // In general, you use Mode.AUTOMATIC
              shareDialog.show(linkContent, ShareDialog.Mode.FEED);
            }
          }
        });

    shareDialog.registerCallback(
        callbackManager,
        new FacebookCallback<Sharer.Result>() {

          @Override
          public void onSuccess(Sharer.Result result) {
            Log.d(TAG, "success");
          }

          @Override
          public void onError(FacebookException error) {
            Log.d(TAG, "error");
          }

          @Override
          public void onCancel() {
            Log.d(TAG, "cancel");
          }
        });
    fbLoginButton.registerCallback(
        callbackManager,
        new FacebookCallback<LoginResult>() {
          @Override
          public void onSuccess(LoginResult loginResult) {
            String id = loginResult.getAccessToken().getUserId();
            profilePictureView.setProfileId(id);
            fbShareButton.setEnabled(true);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("fb_id", id);
            editor.commit();
            getMoreInformation(loginResult);

            Log.d(TAG, "Facebook Login Successful!");
            Log.d(TAG, "Logged in user Details : ");
            Log.d(TAG, "--------------------------");
            Log.d(TAG, "User ID  : " + id);
            Log.d(TAG, "Authentication Token : " + loginResult.getAccessToken().getToken());
            Toast.makeText(MainActivity.this, "Login Successful!", Toast.LENGTH_LONG).show();
          }

          @Override
          public void onCancel() {
            Toast.makeText(MainActivity.this, "Login cancelled by user!", Toast.LENGTH_LONG).show();
            Log.d(TAG, "Facebook Login failed!!");
          }

          @Override
          public void onError(FacebookException e) {
            Toast.makeText(MainActivity.this, "Login unsuccessful!", Toast.LENGTH_LONG).show();
            Log.d(TAG, "Facebook Login failed!!");
          }
        });

    fbLoginButton.addTextChangedListener(
        new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // Log.d(TAG, "beforeTextChanged");
          }

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Log.d(TAG, "onTextChanged");
          }

          @Override
          public void afterTextChanged(Editable s) {
            // Log.d(TAG, "afterTextChanged");
            if (AccessToken.getCurrentAccessToken() == null) {
              clearFbData();
            }
          }
        });
  }