2014-11-05 65 views
0

更新使用Facebook SDK登錄到App

我用Session類所講述。但仍然,我不認爲它的工作。下面是我的代碼:

但是,我的edittext沒有更新名稱。它顯示您還沒有登錄。無論任何在Session.CallBack方法的日誌消息都在我的logcat

public class profile extends Activity implements View.OnClickListener { 

ImageView profilePropic; 
EditText name; 
Dialog dialog; 
Button gallery; 
Bitmap bmp; 
FileOutputStream fos; 
LoginButton loginButton; 
private UiLifecycleHelper uiHelper; 
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions"); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.profile); 

    uiHelper = new UiLifecycleHelper(this, statusCallback); 
    uiHelper.onCreate(savedInstanceState); 

    name = (EditText) findViewById(R.id.profile_name); 
    profilePropic = (ImageView) findViewById(R.id.profile_propic); 

    loginButton = (LoginButton) findViewById(R.id.fb_login_button); 
    loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() { 
     @Override 
     public void onUserInfoFetched(GraphUser user) { 
      if (user != null) { 
       name.setText("Hello, " + user.getName()); 
      } else { 
       name.setText("You are not logged"); 
      } 
     } 
    }); 
} 

private Session.StatusCallback statusCallback = new Session.StatusCallback() { 
    @Override 
    public void call(Session session, SessionState state, Exception exception) { 
     if (state.isOpened()) { 
      Log.d("FacebookSampleActivity", "Facebook session opened"); 
     } else if (state.isClosed()) { 
      Log.d("FacebookSampleActivity", "Facebook session closed"); 
     } 
    } 
}; 


private void beginCrop(Uri source) { 
    Uri outputUri = Uri.fromFile(new File(getCacheDir(), "cropped")); 
    new Crop(source).output(outputUri).asSquare().start(this); 
} 

private void handleCrop(int resultCode, Intent result) { 
    if (resultCode == RESULT_OK) { 

     Uri uri = Crop.getOutput(result); 
     Picasso.with(this).load(uri).into(profilePropic); 
     try { 
      bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      fos = openFileOutput("ProPic", Context.MODE_PRIVATE); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos); 


    } else if (resultCode == Crop.RESULT_ERROR) { 
     Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show(); 
    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    uiHelper.onResume(); 
    FileInputStream fis; 
    try { 
     fis = openFileInput("ProPic"); 
     Bitmap bitmapA = BitmapFactory.decodeStream(fis); 
     profilePropic.setImageBitmap(bitmapA); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

public void changeAvatar(View view) { 
    // Intent pickFromGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    // startActivityForResult(pickFromGallery, OpenGallery); 
    dialog = new Dialog(this); 
    dialog.setContentView(R.layout.propic_dialog); 
    dialog.setTitle("Show us how you look like !"); 
    dialog.show(); 
    gallery = (Button) dialog.findViewById(R.id.gallery); 
    gallery.setOnClickListener(this); 
} 

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

    if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) { 
     beginCrop(data.getData()); 
    } else if (requestCode == Crop.REQUEST_CROP) { 
     handleCrop(resultCode, data); 
    } 
} 

@Override 
public void onClick(View v) { 
    if (v == gallery) { 
     Crop.pickImage(this); 
     dialog.dismiss(); 
    } 
} 

public boolean checkPermissions() { 
    Session s = Session.getActiveSession(); 
    if (s != null) { 
     return s.getPermissions().contains("publish_actions"); 
    } else 
     return false; 
} 

public void requestPermissions() { 
    Session s = Session.getActiveSession(); 
    if (s != null) 
     s.requestNewPublishPermissions(new Session.NewPermissionsRequest(
       this, PERMISSIONS)); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    uiHelper.onPause(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    uiHelper.onDestroy(); 
} 

@Override 
public void onSaveInstanceState(Bundle savedState) { 
    super.onSaveInstanceState(savedState); 
    uiHelper.onSaveInstanceState(savedState); 
    } 
} 

我哈弗研究了這個tipic相當長的一段所示。

我遇到什麼,在很多的教程,而在許多其他的網站,它是說,Facebook類是現在已經過時

Facebook fb = new Facebook(App_Id); 

使用。在Facebook開發者頁面也使用Session類。那是什麼以及如何使用它?

這是我的第一個應用程序。如果有人指導我詳細瞭解這個概念,那對我來說會很有幫助。

謝謝

回答

0

此類應考慮廢棄。

此類的所有公共成員都被故意棄用。新代碼應改爲使用Session來管理會話狀態,請求進行API請求,並使用WebDialog進行對話請求。

將@Deprecated添加到此類會導致引用此類的其他棄用類中的警告。這是整個班級不被棄用的唯一原因。

閱讀下面的文檔就棄用: -

https://developers.facebook.com/docs/reference/android/current/class/Facebook/

更新Facebook的SDK從下面的鏈接: -

https://developers.facebook.com/docs/android/upgrading

+0

我使用Session類,但還是有一些問題,我更新我的問題。 Plz看看 – 2014-11-05 06:01:04