ShareDialog不会打开Facebook应用程序

时间:2016-08-04 08:32:22

标签: android facebook

尝试实现Facebook共享,但是如果我设置ShareDialog.Mode.AUTOMATIC,它只适用于webview,如果我将它设置为NATIVE则根本没有任何显示,我也没有收到任何错误回调。 Facebook应用程序安装在模拟器中。

清单中的提供商:

<provider android:authorities="com.facebook.app.FacebookContentProvider{myAppId}"
        android:name="com.facebook.FacebookContentProvider"
        android:exported="true"/>

分享代码:

public void share(String contentUrl,
                  Activity activity){
        ShareLinkContent content = new ShareLinkContent.Builder()
            .setContentUrl(Uri.parse(contentUrl))
            .build();
    ShareDialog dialog = new ShareDialog(activity);
    dialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
        @Override
        public void onSuccess(Sharer.Result result) {
                EventBus.getDefault().post(new ShareEvent(true));
        }

        @Override
        public void onCancel() {
            Log.e(TAG, "Facebook Share Cancel");
            EventBus.getDefault().post(new ShareEvent(false));
        }

        @Override
        public void onError(FacebookException error) {
            Log.e(TAG, "Facebook share error: " + error.getMessage());
            EventBus.getDefault().post(new ShareEvent(false));
        }
    })
    dialog.show(content, ShareDialog.Mode.NATIVE);
}

我正在从片段中调用share(..)方法。

我尝试使用意图实现共享,这是有效的

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://myUrl.com");
    PackageManager pm = activity.getPackageManager();
    List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
    for (final ResolveInfo app : activityList) {
        if ((app.activityInfo.name).contains("facebook")) {
            final ActivityInfo mActivity = app.activityInfo;
            final ComponentName name = new ComponentName(mActivity.applicationInfo.packageName, mActivity.name);
            shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            shareIntent.setComponent(name);
            activity.startActivity(shareIntent);
            break;
        }
    }

但是在我的情况下,我需要成功的共享回调,我知道我的最后一个解决方案没有提供,因为它总是返回结果CANCELLED

1 个答案:

答案 0 :(得分:1)

您是否已获得许可?如下所示:

FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();

List<String> permissionNeeds = Arrays.asList("publish_actions"); // permission.
loginManager = LoginManager.getInstance();

loginManager.logInWithPublishPermissions(this, permissionNeeds);

loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
      @Override
      public void onSuccess(LoginResult loginResult) {
             // do something here
             finish();
      }

      @Override
      public void onCancel() {}

      @Override
      public void onError(FacebookException exception) {}
});

这是我的示例代码:https://github.com/minhhuy150894/simple-puzzle/blob/master/app/src/main/java/xyz/davidng/puzzle/FacebookShareImage.java