好的,这是我使用 Facebook 的 3.0 SDK for Android 制作的对话框片段。它是封装的,这意味着所有 Facebook 功能都包含在片段中。这是一个对话框片段,因此它会在您正在运行的活动中弹出。此片段允许您登录,然后在您使用 Facebook 中包含的 UiHelper 类登录后启用某些视图。
public class FFragment extends DialogFragment {
private static final String TAG = "FacebookFragment";
private UiLifecycleHelper uiHelper;
private String mFilePath;
private Button shareButton, cancelButton;
private EditText mMessageText;
private TextView mMessageTitle;
private ProgressBar pBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.facebook_login, container, false);
LoginButton authButton = (LoginButton) view
.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setPublishPermissions(Arrays.asList("publish_stream"));
shareButton = (Button) view.findViewById(R.id.shareButton);
mMessageText = (EditText) view.findViewById(R.id.facebook_post_text);
mMessageTitle = (TextView) view.findViewById(R.id.facebook_post_title);
cancelButton = (Button) view.findViewById(R.id.cancelButton);
pBar = (ProgressBar) view.findViewById(R.id.facebook_pbar);
cancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
FFragment.this.dismiss();
}
});
shareButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
publishPhoto();
}
});
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFilePath = getArguments().getString("file_path");
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
/**
* After user selects to upload photo
*/
private void publishPhoto() {
pBar.setVisibility(View.VISIBLE);
GraphObject graphObject;
Bitmap bmap = BitmapFactory.decodeFile(mFilePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
Bundle params = new Bundle();
params.putByteArray("picture", byteArray);
params.putString("message", mMessageText.getText() + " " +
getActivity().getResources().getString("String goes here"));
Request request = new Request(Session.getActiveSession(), "me/photos",params,
HttpMethod.POST);
request.setCallback(new Request.Callback() {
@Override
public void onCompleted(Response response) {
if (response.getError()==null) {
Toast.makeText(getActivity(), "Successfully posted photo", Toast.LENGTH_SHORT).show();
FlurryAgent.logEvent(Globals.EVENT_FACEBOOK);
} else {
Toast.makeText(getActivity(), response.getError().getErrorMessage(), Toast.LENGTH_SHORT).show();
}
pBar.setVisibility(View.GONE);
FFragment.this.dismiss();
}
});
request.executeAsync();
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
// Check for reading user_photos permission
shareButton.setVisibility(View.VISIBLE);
mMessageText.setVisibility(View.VISIBLE);
mMessageTitle.setVisibility(View.VISIBLE);
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
shareButton.setVisibility(View.GONE);
mMessageText.setVisibility(View.GONE);
mMessageTitle.setVisibility(View.GONE);
}
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
以下代码行是您将在活动中用于显示对话框的代码。
FFragment mFacebookFragment = new FFragment();
Bundle args = new Bundle();
args.putString("file_path", mFilePath);
mFacebookFragment.setArguments(args);
mFacebookFragment.show(getSupportFragmentManager(), "tag");
无论如何,如果您查看 onCreate 我们正在设置发布权限。默认情况下,片段启动时已经设置了读取权限。
也让你在清单中有这个。
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
其中@string/app_id 是您在开发者网站上创建 facbeook 应用程序时创建的应用程序 ID。还要确保下载新的 sdk facebook 项目并将其作为库项目引用。用于对话框布局的 XML 文件。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_gravity="center_horizontal|top" />
<TextView
android:id="@+id/facebook_post_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Message to post with photo"
android:textColor="@android:color/white"
android:textSize="18dp"
android:textStyle="bold"
android:visibility="gone" />
<EditText
android:id="@+id/facebook_post_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:visibility="gone" />
<Button
android:id="@+id/shareButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="Post"
android:textStyle="bold"
android:visibility="gone" />
<Button
android:id="@+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Cancel"
android:textStyle="bold" />
</LinearLayout>
<ProgressBar
android:id="@+id/facebook_pbar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:visibility="gone"
/>