如何在Twitter上分享字符串消息?我想让用户在多个字符串消息之间进行选择,然后单击一个以通过Twitter分享.
rid*_*doy 16
有很多方法可以实现这一点.可以简单地打开http://twitter.com并在单个意图中分享推文,例如..
Intent tweet = new Intent(Intent.ACTION_VIEW);
tweet.setData(Uri.parse("http://twitter.com/?status=" + Uri.encode(message)));//where message is your string message
startActivity(tweet);
Run Code Online (Sandbox Code Playgroud)
要么,
Intent tweet = new Intent(Intent.ACTION_SEND);
tweet.putExtra(Intent.EXTRA_TEXT, "Sample test for Twitter.");
startActivity(Intent.createChooser(share, "Share this via"));
Run Code Online (Sandbox Code Playgroud)
要么,
String tweetUrl = "https://twitter.com/intent/tweet?text=WRITE YOUR MESSAGE HERE &url="
+ "https://www.google.com";
Uri uri = Uri.parse(tweetUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
Run Code Online (Sandbox Code Playgroud)
要通过 twitter 共享,我在 Util 类中使用我自己的自定义静态函数,如下所示:
public class Util
{
public static Intent getTwitterIntent(Context ctx, String shareText)
{
Intent shareIntent;
if(doesPackageExist(ctx, "com.twitter.android"))
{
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setClassName("com.twitter.android",
"com.twitter.android.PostActivity");
shareIntent.setType("text/*");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareText);
return shareIntent;
}
else
{
String tweetUrl = "https://twitter.com/intent/tweet?text=" + shareText;
Uri uri = Uri.parse(tweetUrl);
shareIntent = new Intent(Intent.ACTION_VIEW, uri);
return shareIntent;
}
}
}
Run Code Online (Sandbox Code Playgroud)
此功能的优点是,如果安装了 twitter 应用程序,它将使用该应用程序,否则,它将使用基于 Web 的 twitter 页面。将在推特上发布的文本被传递到函数中。
在您的场景中,当用户从一组消息中进行选择时,将所选消息传递到函数中。然后它会给你一个 Intent,你可以像这样插入 startActivity() 函数:
startActivity(Util.getTwitterIntent(context, "Text that will be tweeted"));
Run Code Online (Sandbox Code Playgroud)