如何在Android中通过Twitter分享链接?

时间:2014-10-28 13:07:49

标签: android twitter

在我的应用程序中,我需要通过Facebook和Twitter分享促销网站的链接,在Facebook中,我们得到类似"share dialog"的内容或发布捆绑类似

Request request = new Request(Session.getActiveSession(), "me/feed", bundle, HttpMethod.POST, 
       new Request.Callback(){
                @Override
                public void onCompleted(Response response) {
                    ...
                }
            });

但没有针对Android的twitter sdk(我使用twitter4j)并且无法发布包

那么如何通过推特发送推文链接?

3 个答案:

答案 0 :(得分:2)

如果您发送带有&#34之类链接的文字;这是我们的链接:http://stackoverflow.com" - twitter会理解它并从您的链接中获取信息并显示它 - 据我所知,您需要在Twitter Feed中查看您认可的链接。 要发布链接,您可以像@Adrian Sicaru一样制作意图,或者在提到时使用twitter4j使用asynctask创建自己的自定义对话框:

@Override
protected Bundle doInBackground(Bundle... params) {
    Bundle args = params[0];
    Bundle result = new Bundle();

    String paramMessage = args.getString(MESSAGE);
    String paramLink = args.getString(LINK);

    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey("your ConsumerKey");
    builder.setOAuthConsumerSecret("your ConsumerSecret");

    String accessToken = "your Token";
    String accessTokenSecret = "your Secret";

    TwitterFactory factory = new TwitterFactory(builder.build());
    mTwitter = factory.getInstance(new AccessToken(accessToken, accessTokenSecret));
    try {
        StatusUpdate status = new StatusUpdate(paramMessage);

        if (paramLink != null) {
            status = new StatusUpdate(paramMessage + " " + paramLink);
        }
        mTwitter.updateStatus(status);
    } catch (TwitterException e) {
        result.putString(RESULT_ERROR, e.getMessage());
    }
    return result;
}

答案 1 :(得分:1)

你可以通过这样的意图来做到这一点:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

这将打开一个已经安装了可以回答意图的应用程序的列表,例如gmail,facebook和... twitter。

您可以在此处找到更多信息:http://developer.android.com/training/sharing/send.html

此外,如果您想直接致电推特并且不想选择,请查看this answer

答案 2 :(得分:1)

就像@TribblerWare回答只是在推文中写链接并且推特认可它。

顺便说一句,你可以使用ASNE library - 只需授权用户并使用 requestPostLink 并将bundle作为参数

Bundle postParams = new Bundle(); 
postParams.putString(SocialNetwork.BUNDLE_LINK, link); 
socialNetwork.requestPostLink(postParams, message, postingComplete);

您可以在tutorial

中看到更详细的信息