首页 文章

通过ACTION_SEND在Android应用中分享Facebook上的文字

提问于
浏览
91

我有一个Android应用程序,它支持通过其他应用程序发送文本 . 因此,它使用ACTION_SEND意图和EXTRA_TEXT字段 . 选择器向我提供了可以处理这种意图的所有应用程序 . 这些是推特,电子邮件,...和Facebook . 但是当我选择Facebook时,它会打开浏览器并转到以下页面:

http://m.facebook.com/sharer.php?u=mytext

它显示我的文本和提交按钮 . 但是,当我按下提交按钮时,没有任何事情发生 . 页面再次加载 . 我想也许只能通过Facebook应用程序发送URL . 可能是吗?

有没有人设法通过Facebook Android应用程序通过ACTION_SEND发送文本?

10 回答

  • 11

    编辑:随着Android官方Facebook应用程序的新版本发布(2011年7月14日) IT WORKS!!!

    OLD:如果用户选择Facebook应用程序进行共享,则上述示例不起作用,但如果用户选择Seesmic应用程序发布到Facebook,则它们可以正常工作 . 我猜Seesmic比Facebook有更好的Facebook API实现!

  • 45

    要使Share与facebook应用程序一起使用,您只需要至少有一个链接:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, "Wonderful search engine http://www.google.fr/");
    startActivity(Intent.createChooser(intent, "Share with"));
    

    这将显示正确的共享窗口,但是当您单击共享时,没有任何事情发生(我也尝试使用官方Twitter应用程序,它不起作用) .

    我发现使Facebook应用程序共享工作的唯一方法是仅共享没有文本的链接:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, "http://www.google.fr/");
    startActivity(Intent.createChooser(intent, "Share with"));
    

    它将显示以下窗口,共享按钮将起作用:

    facebook share

    显然,它会自动从链接中获取图像和文本以填充共享 .

    如果你只想分享文字,你将不得不使用facebook api:https://github.com/facebook/facebook-android-sdk

  • 2

    06/2013:

    • 这是来自Facebook的错误,而不是您的代码

    • Facebook will NOT fix this bug ,他们说 it is "by design" 他们打破了Android共享系统:https://developers.facebook.com/bugs/332619626816423

    • 使用SDK或仅共享URL .

    • 提示:您可以使用网页 Headers 作为帖子的文本作弊 .

  • 51

    首先,您需要查询Intent以处理程序共享选项 . 然后使用包名来过滤Intent然后我们将只有一个Intent那个处理程序共享选项!

    Share via Facebook

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

    Bonus - Share via Twitter

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

    如果你想找到如何通过另一个共享应用程序共享,请在那里找到它Tép Blog - Advance share via Android

  • 0

    所以我有一个解决方法,但它假设您可以控制您共享的页面...

    如果你格式化你的EXTRA_TEXT ......

    String myText = "Hey!\nThis is a neat pic!";
    String extraText = "http://www.example.com/myPicPage.html?extraText=\n\n" + myText;
    

    ...然后在非Facebook应用程序上,您的文本应该如下所示:

    http://www.example.com/myPicPage.html?extraText=嘿!这是一张整洁的照片!

    现在,如果您更新您的网站,使用extraText查询参数的请求将返回页面元数据中extraText的内容 .

    <!-- Make sure to sanitize your inputs! e.g. http://xkcd.com/327/ -->
    <meta name="title" content="Hey! this is a neat pic!">
    

    然后,当Facebook逃离该URL以生成对话框时,它将读取 Headers 元数据并将其嵌入到您的共享对话框中 .

    我意识到这是一个非常糟糕的解决方案,所以带上一粒盐......

  • 1

    似乎Facebook应用程序错误地处理了这个意图 . 最可靠的方式似乎是使用适用于Android的Facebook API .

    SDK位于以下链接:http://github.com/facebook/facebook-android-sdk

    在'使用'下,有这样的:

    显示Facebook对话框 . SDK支持多个用于用户交互的WebView html对话框,例如创建墙贴 . 这旨在提供快速Facebook功能,而无需实现原生Android UI并通过API直接将数据传递到Facebook .

    这似乎是最好的方法 - 显示一个会发布到墙上的对话框 . 唯一的问题是他们可能必须先登录

  • 11
    Check this out : By this we can check activity results also....
    // Open all sharing option for user
                        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
                        sharingIntent.setType("text/plain");                    
                        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, ShortDesc+" from "+BusinessName);
                        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, ShortDesc+" "+ShareURL);
                        sharingIntent.putExtra(Intent.EXTRA_TITLE, ShortDesc+" "+ShareURL);
                        startActivityForResult(Intent.createChooser(sharingIntent, "Share via"),1000);
    /**
         * Get the result when we share any data to another activity 
         * */
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            switch(requestCode) {
            case 1000:
                if(resultCode == RESULT_OK)
                    Toast.makeText(getApplicationContext(), "Activity 1 returned OK", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(getApplicationContext(), "Activity 1 returned NOT OK", Toast.LENGTH_LONG).show();
                break;
            case 1002:
                if(resultCode == RESULT_OK)
                    Toast.makeText(getApplicationContext(), "Activity 2 returned OK", Toast.LENGTH_LONG).show();
                break;
            }// end switch
    
    
    
        }// end onActivityResult
    
  • 28
    ShareDialog shareDialog = new ShareDialog(this);
    if(ShareDialog.canShow(ShareLinkContent.class)) {
    
        ShareLinkContent linkContent = new ShareLinkContent.Builder().setContentTitle(strTitle).setContentDescription(strDescription)
                                .setContentUrl(Uri.parse(strNewsHtmlUrl))
                                .build();
        shareDialog.show(linkContent);
    
    }
    
  • 1

    它似乎是2011年4月报道的Facebook应用程序中的一个错误,但仍未被Android Facebook开发者修复 .

    目前唯一的解决方法是使用他们的SDK .

  • 0

    如果你想在你想要的消息的乞讨时显示文字 # 它会将它作为Hashtag分享

相关问题