Tôi hiện đang thực hiện một dự án nhỏ muốn có nút chia sẻ (hộp thoại), khi người dùng nhấp vào nó, nó sẽ tự động đăng nhập vào tài khoản fb của anh ta/cô ấy và chia sẻ nội dung họ muốn.Facebook SDK Android 3.0, cách chia sẻ nội dung mà không có LoginButton
Sau hướng dẫn từ fb dev, ứng dụng của tôi có thể chia sẻ nội dung lên tường, nhưng cần đăng nhập bằng nút fblogin trước khi chia sẻ.
Tôi đã đọc một bài đăng từ stackoverflow: Android - Facebook SDK 3 - How to login programmatically without LoginButton
CẬP NHẬT: Tôi có thực hiện feedDialog với onActivityResult trong dự án của tôi, tôi thấy rằng tôi có thể đăng nhập và chia sẻ với một nút bấm. HOWEVER, khi tôi xây dựng lại ứng dụng/khởi động lại điện thoại, tôi phải nhấn nút hai lần để chia sẻ lần đầu tiên, nhưng trở thành bình thường (nhấn một lần) sau.
P.S.I có thực hiện nó với ShareActionProvider
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.content_main, menu);
/** Getting the actionprovider associated with the menu item whose id is share */
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
/** Getting the target intent */
Intent intent = getDefaultShareIntent();
/** Setting a share intent */
if(intent!=null){
mShareActionProvider.setShareIntent(intent);
mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener(){
@Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
if ("com.facebook.katana".equals(intent.getComponent().getPackageName())){
if (Session.getActiveSession() == null || Session.getActiveSession().isClosed()) {
Session.openActiveSession(Content.this, true, null);
}else{
publishFeedDialog();
}
return true;
}
return false;
}
});
}
return super.onCreateOptionsMenu(menu);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
if (Session.getActiveSession() != null || Session.getActiveSession().isOpened())
publishFeedDialog();
}
private void publishFeedDialog() {
Bundle params = new Bundle();
params.putString("name", "ab");
params.putString("caption", "cd");
params.putString("description", "def");
params.putString("link", "https://developers.facebook.com/android");
params.putString("picture", "abc.jpg");
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(Content.this,
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(Content.this,
"Posted story, id: "+postId,
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(Content.this.getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(Content.this.getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Generic, ex: network error
Toast.makeText(Content.this.getApplicationContext(),
"Error posting story",
Toast.LENGTH_SHORT).show();
}
}
})
.build();
feedDialog.show();
}
Đi qua Ming Li câu trả lời và bình luận của ông (thứ hai). –
tôi biết, nhưng tôi thực sự muốn giải thích thêm một chút hoặc mã becox tôi không hiểu ........ i am mới cho android dev –
@CalvinChan bạn đã có thể để kéo này đi? Tôi đang tìm giải pháp tương tự mà tôi sẽ chia sẻ hình ảnh – Casper