gpt4 book ai didi

java - Facebook 与fragment android 共享对话框

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:24 24 4
gpt4 key购买 nike

我构建了一个简单的应用程序,允许用户通过 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();
}
}

问题是当用户单击共享按钮时,会出现一个共享对话框,但没有链接并立即消失,我在没有 fragment 的 Activity 中尝试了代码,效果很好。

我也以正确的方式制作了 list 文件。

下面的代码是 facebook 对话框代码:

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

最佳答案

在最新的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

关于java - Facebook 与fragment android 共享对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28520477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com