Android - 共享

标签 android facebook twitter share

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

最佳答案

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

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

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

只需使用以下行开始 Intent :

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 打开了哪个应用吗?

不要相信内置的 Intent 选择器,制作你自己的对话并给每个 Intent 另一个requestCode(一个唯一的整数值,用于识别 Intent )

一个例子:

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或者如果您对我的代码有建议(我总是喜欢反馈 ^^)或者如果您卡在某事上:) 或发表评论

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

关于Android - 共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9701959/

相关文章:

javascript - 使用 Facebook ://To Share a Post through the Facebook App with Web Interface

php - 在没有页面加载的情况下检索数据库值 AJAX/PHP/Facebook 样式

ruby-on-rails - omn​​iauth 模拟 facebook 回复

python - 使用 Python 通过 Twitter API 引用推文

Twitter 分享按钮不显示推特卡片

php - 如何正确保存和显示表情符号推文,如\udc3b

android:手动处理配置更改的优点/缺点

android - 数据绑定(bind)中的开关盒

android - listView 显示所有数据,而不仅仅是我指定的数据

java - match_parent 布局参数不起作用?