创建"推文此按钮"仅在Twitter上分享信息并非易事

Waz*_*_Be 5 twitter android android-intent

对于我的应用程序,我在主屏幕中显示一个twitterfeed.最酷的是,人们可以在没有Twitter帐户的情况下关注我所在国家的火车公司的推特信息.

在此输入图像描述

我想做的是当用户点击某个项目然后允许他回复/ RT时作出反应.如果用户没有安装推特应用程序,那么什么都不应该发生(也许是吐司)

我试过这个动作:

Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "Test; please ignore");
tweetIntent.setType("application/twitter");
Run Code Online (Sandbox Code Playgroud)

但似乎还要打开电子邮件,Gmail和其他无用的应用程序.

有没有办法过滤该列表,只保留Twitter应用程序(Twitter,Twydroid,Plume,Twicca等...)

非常感谢任何建议.

编辑:我目前的解决方法,我不喜欢AT ALL:

public Intent findTwitterClient() {
        final String[] twitterApps = {
                // package // name
                "com.twitter.android", // official
                "com.levelup.touiteur", // Plume 
                "com.twidroid", // twidroyd
                "com.handmark.tweetcaster", //
                "com.thedeck.android" //  };
        Intent tweetIntent = new Intent();
        tweetIntent.setType("text/plain");
        final PackageManager packageManager = getPackageManager();
        List<resolveinfo> list = packageManager.queryIntentActivities(
                tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);

        for (int i = 0; i <twitterApps.length; i++) {
            for (ResolveInfo resolveInfo : list) {
                String p = resolveInfo.activityInfo.packageName;
                if (p != null && p.startsWith(twitterApps[i])) {
                    tweetIntent.setPackage(p);
                    return tweetIntent;
                }
            }
        }
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

bbe*_*ard 1

android 中没有官方的 twitter API,我强烈建议您使用像 twitter4j 这样的东西来实现自己的 API。

http://twitter4j.org/en/index.html

你应该知道点击时的用户 ID 和推文 ID 等,然后你可以像这样使用 twitter4j:

Twitter twitter = new TwitterFactory().getInstance();
twitter.updateStatus(new StatusUpdate("@USERNAME").inReplyToStatusId(STATUSID)); 
Run Code Online (Sandbox Code Playgroud)

我不确定您是否已经在使用 twitter4j,如果您没有,我强烈建议您这样做,因为查询推文非常容易。

如果您使用 twitter4j,您还需要在应用程序中进行身份验证,这相当简单,但我不会深入讨论,因为您需要弹出一个身份验证窗口等。http: //twitter4j.org/en/有一个示例代码示例.html


如果您不想走 twitter4j 路线,不幸的是您的路线是我所知道的唯一路线。只需使用包管理器检查它们是否存在,否则通知用户。