Facebook与片段android共享对话框

时间:2015-02-14 21:23:43

标签: java android android-activity android-fragments facebook-sdk-3.0

我构建了一个简单的应用程序,允许用户通过Facebook共享链接。

在我的代码下面:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.ContentProviderOperation;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.FacebookDialog;

public class DownloadFragment extends Fragment {
    ImageView fbshare;
    private UiLifecycleHelper uiHelper;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.download, container, false);
          if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }
          uiHelper = new UiLifecycleHelper(getActivity(),null);
            uiHelper.onCreate(savedInstanceState);
          fbshare = (ImageView) rootView.findViewById(R.id.facebookimage);
          fbshare.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View arg0) {
               FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(getActivity())
               .setLink("https://www.test.com")
               .build();
       uiHelper.trackPendingDialogCall(shareDialog.present());
         }
      });

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

        uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
            @Override
            public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
                Log.e("Activity", String.format("Error: %s", error.toString()));
            }

            @Override
            public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
                Log.i("Activity", "Success!");
            }
        });
    }

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

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

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

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

问题是,当用户单击共享按钮时,没有链接显示共享对话框并立即消失,我在没有片段的活动中尝试了代码,并且工作正常。

我也以正确的方式完成了清单文件。

以下代码是facebook对话框代码:

FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(getActivity())
.setLink("https://www.test.com").build();
uiHelper.trackPendingDialogCall(shareDialog.present());

1 个答案:

答案 0 :(得分:0)

在最新的SDK中有一种更简单的方法

1)添加分享按钮

2)创建内容

3)将其分配给按钮

代码:

    Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.myimage);
    SharePhoto photo = new SharePhoto.Builder()
            .setBitmap(image)
            .build();
    SharePhotoContent sharePhotoContent = new SharePhotoContent.Builder()
            .addPhoto(photo)
            .build();

    ShareButton FBShare = (ShareButton) v.findViewById(R.id.fb_share);
    FBShare.setShareContent(sharePhotoContent);

XML:

                <com.facebook.share.widget.ShareButton
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:id="@+id/fb_share"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true"
                    android:padding="4dp" />

文件:

Sharing on Android