【问题标题】:Android - sharingAndroid - 共享
【发布时间】:2012-03-30 21:53:19
【问题描述】:

是否可以列出 Intent.ACTION_SEND ? 我的意思是我需要知道是否有人通过 action_send 在 Facebook 上分享或在 Twitter 上发推文。

【问题讨论】:

  • 你到底想实现什么?
  • 我有“分享”按钮,用于在用户的 Facebook 墙上发布内容,并且我有 DialogListener 来了解用户是否真的分享或只是按下按钮并退出。我希望有可能也分享到 Twitter 或只是短信,但我需要知道用户是否真的分享以增加他的积分。

标签: android facebook twitter share


【解决方案1】:

也许您想要一个更完整的答案,因为接受的答案很短,我已经晚了一年,但希望它仍然有用:)

所以这是处理多个意图的可能解决方案...

1) 您想知道意图的结果(例如成功或失败)?

只需使用以下行开始意图:

startActivityForResult(intent, 1); //instead of startActivity(intent)

并通过覆盖 onActivityResult 来检索 requestCode 和 resultCode:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
        if(resultCode == Activity.RESULT_OK){
            //intent 0 = succesful (Facebook)
        } else{
            //intent 0 = failed or canceled
        }
    } else if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            //intent 1 = succesful (Twitter)
        } else{
            //intent 1 = failed or canceled
        }
    }
}

2) 您想知道 Intent 打开了哪个应用?

不要相信内置的意图选择器,创建自己的对话并为每个意图赋予另一个 requestCode(唯一的整数值,用于识别意图)

一个例子:

new AlertDialog.Builder(this)
.setTitle("Share with friends!")
.setSingleChoiceItems(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,
    new String[]{"Facebook", "Twitter"}), -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    StartFacebookShare();
                } else if (which == 1) {
                    StartTwitterShare();
                }
                dialog.dismiss();
            }
}).show();

private void StartFacebookShare() {
    String messageUrl = "http://www.stackoverflow.com"; //the url you want to share
    try {
        Intent intent = new Intent("android.intent.category.SEND");
        intent.putExtra(Intent.EXTRA_TEXT, messageUrl);
        intent.setClassName("com.facebook.katana", "com.facebook.katana.ShareLinkActivity");
        startActivityForResult(intent, 0);
    } catch (Exception e) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://m.facebook.com/sharer.php?u=" + messageUrl));
        startActivityForResult(intent, 1);
    }
}
private void StartTwitterShare() {
    String messageUrl= "http://www.stackoverflow.com"; //the string you want to tweet
    try {
        //see edit below for more info on API
        Twitter twitter = TwitterFactory.getSingleton();
        Status status = twitter.updateStatus(messageUrl);
    } catch (Exception e) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://twitter.com/intent/tweet?text=" + messageUrl));
        startActivityForResult(intent, 1);
    }
}

可以在herehere 找到一些有用的信息,如果您对我的代码有任何建议(我总是喜欢反馈^^)或者如果您遇到问题,可以搜索here 或发表评论 :)

编辑:一些小的变化,比如意图,总是添加一个带有网络意图的捕获(现在不能失败,对吧?) 对于 Twitter 部分,我使用了 jar(将其放在您的“libs”文件夹中)和需要关注 registration 的 Twitter API 和 configuration,祝你好运!

【讨论】:

  • 一段不错的代码。现在不需要它,但我肯定将来会需要它!谢谢
  • 永远不要硬编码来自第三方应用程序的类和包名称,这些应用程序不属于某些记录和支持的 API。
  • @CommonsWare 同意。看编辑,这有点像你想到的解决方案吗?
【解决方案2】:

不,您无法确定某人是否真的在您通过ACTION_SEND 链接到的应用程序中执行了任何操作。这与网络非常相似,您不知道一个人是否在您通过 URL 链接到的网站中做了任何事情。

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 2012-01-30
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多