1

正如标题所暗示的,我想在 android 中通过 facebook/twitter 分享一些东西。如果安装了 facebook/twitter,我想通过 fb/twitter 应用程序分享帖子,否则我想将用户定向到浏览器. 我可以将用户引导至浏览器,这很容易,但我如何通过 fb/twitter 应用程序分享帖子?

谢谢..

(已编辑)我尝试过这样做,检查了 developers.facebook.com ,但是他们通过片段进行此操作。我想用默认活动来做。我已经尝试了下面的代码,但是我收到了类似的错误

W/dalvikvm(16093): VFY: unable to find class referenced in signature     (Landroid/support/v4/app/Fragment;)
W/dalvikvm(16093): VFY: unable to find class referenced in signature (Landroid/support/v4/app/Fragment;)
W/dalvikvm(16093): VFY: unable to resolve static method 367: Landroid/support/v4/content/LocalBroadcastManager;.getInstance (Landroid/content/Context;)Landroid/support/v4/content/LocalBroadcastManager;

我在默认活动中使用的代码是:

private void publishStory() {
        Session currentSession = Session.getActiveSession();


        if (currentSession != null){


            Bundle postParams = new Bundle();
            postParams.putString("name", Commons.campaignname);
            postParams.putString("display", "touch");
            postParams.putString("link",  Commons.campaignlink");
            postParams.putString("picture",Commons.campaignimage);

            Request.Callback callback= new Request.Callback() {
                public void onCompleted(Response response) {
                    JSONObject graphResponse = response
                                               .getGraphObject()
                                               .getInnerJSONObject();
                    String postId = null;
                    try {
                        postId = graphResponse.getString("id");
                    } catch (JSONException e) {
                        Log.i("TAG",
                            "JSON error "+ e.getMessage());
                    }
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Toast.makeText(getApplicationContext(),
                             error.getErrorMessage(),
                             Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplicationContext(), 
                                 postId,
                                 Toast.LENGTH_LONG).show();
                    }
                }
            };

            Request request = new Request(currentSession, "me/feed", postParams, 
                                  HttpMethod.POST, callback);

            RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();
        }

    }

    private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
        for (String string : subset) {
            if (!superset.contains(string)) {
                return false;
            }
        }
        return true;

}
4

2 回答 2

2

使用 Facebook 发布内容而不是集成 Facebook android sdk

开始使用适用于 Android 的 Facebook SDK

使用 Feed Dialog 在 Facebook 上发帖。如果已安装,它将自动打开 fb 应用程序,否则在浏览器中打开

贴到墙上

public class PhotoViewer extends Activity implements StatusCallback{

    Button btnShare;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_viewer);

        btnShare = (Button) findViewById(R.id.btnShare);

        btnShare.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Session.openActiveSession(PhotoViewer.this, true, PhotoViewer.this);

            }
        });

    }

    @Override
    public void call(Session session, SessionState state, Exception exception) {

        if (session.isOpened()) {
            publishFeedDialog();
        }
    }

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

    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(PhotoViewer.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(PhotoViewer.this,"Posted story, id: "+postId,Toast.LENGTH_SHORT).show();
                                        Toast.makeText(getApplicationContext(), "Publish Successfully!", Toast.LENGTH_SHORT).show();
                                    } else {
                                        // User clicked the Cancel button
                                        Toast.makeText(getApplicationContext(), "Publish cancelled", Toast.LENGTH_SHORT).show();
                                    }
                                } else if (error instanceof FacebookOperationCanceledException) {
                                    // User clicked the "x" button
                                    Toast.makeText(getApplicationContext(), "Publish cancelled", Toast.LENGTH_SHORT).show();
                                } else {
                                    // Generic, ex: network error
                                    Toast.makeText(getApplicationContext(),"Error posting story",Toast.LENGTH_SHORT).show();
                                }
                            }

                        })
                        .build();
        feedDialog.show();
    }

}

如果您不使用 Fragment 而不是使用ClassName.this 之类的 Context而不是getActivity()

编辑

请在 onCreate() 中添加此代码以打印哈希键

// Add code to print out the key hash
PackageInfo info = getPackageManager().getPackageInfo("com.your.package", // replace with your package name 
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());

        Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));

    }
于 2013-06-18T08:50:46.710 回答
0

通过此代码,您可以检查设备上是否安装了 facebook 或 twitter 应用程序。如果条件为假,休息你可以把你的逻辑

final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm
            .getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
    Log.d(TAG, "Installed package : " + packageInfo.packageName);
}

然后您可以检查是否packageInfo.packageName等于包含该应用程序包名称的某个字符串。

于 2013-06-18T14:45:10.347 回答