[英]Sharing on facebook without the native app installed
我正在嘗試在Facebook上分享一些東西。 遵循本指南 ,當您擁有本機應用程序時,我設法完成了共享。 但是,當我到達此位置時:
步驟4:使用Feed對話框作為后備廣告。 它說要使用后備方法,我需要在下面添加代碼
private void publishFeedDialog() {
Bundle params = new Bundle();
params.putString("name", "Facebook SDK for Android");
params.putString("caption", "Build great social apps and get more installs.");
params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
params.putString("link", "https://developers.facebook.com/android");
params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(getActivity(),
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(getActivity(),
"Posted story, id: "+postId,
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(getActivity().getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(getActivity().getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Generic, ex: network error
Toast.makeText(getActivity().getApplicationContext(),
"Error posting story",
Toast.LENGTH_SHORT).show();
}
}
})
.build();
feedDialog.show();
}
添加getActivity()
之后,我在getActivity()
遇到錯誤,我使用MyClass.this
修復了這個問題。 然后,當我運行該應用程序(我在啟動時直接運行此方法)時,我的應用程序崩潰了,並且出現了一些錯誤,例如“試圖使用未打開的會話”
沒有人知道如何在不安裝本機應用程序的情況下共享內容嗎?
導致該錯誤的原因是該用戶尚未通過您的應用登錄到Facebook。 您將需要在應用程序中的某處實現使用Facebook登錄。 即使用戶未安裝本機應用程序,Facebook SDK也會回退到Web對話框上以請求用戶登錄。因此,您將需要檢查會話是否已打開(即用戶已登錄)。
Session session = Session.getActiveSession();
boolean isSessionOpen = (session != null && session.isOpened());
if(!isSessionOpen) {
Toast.makeText(getApplicationContext(),"Please Login With Facebook First!", Toast.LENGTH_SHORT).show();
return;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.