启动Twitter应用

时间:2011-04-08 12:16:56

标签: android android-intent twitter

  

可能重复:
  Android Intent for Twitter application

我想从我的Android应用程序中关注页面,我希望通过启动本机Twitter应用程序来完成此操作。

我怎么能这样做呢?如果用户的手机中没有Twitter客户端,它应该进入twitter的移动网站。

4 个答案:

答案 0 :(得分:5)

要检查Intent是否存在,请尝试:

public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

source

对于twitter,请查看以下代码段:

Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "Test tweet");
tweetIntent.setType("application/twitter");

source

答案 1 :(得分:5)

从我的测试中,我找不到一个很好的方法来做到这一点,而是求助于一个不一定是“最佳实践”的解决方案。它仅适用于官方Twitter应用程序,而不适用于其他应用程序。如果官方应用程序更改其内部API,此解决方案将失败。因此,请谨慎使用此解决方案并了解其局限性。

这段代码写得不好,但确实有效。我的建议是将其更改为不使用这么多资源。

代码检查是否安装了Twitter应用程序。如果是这样,推出Twitter应用程序;否则,浏览器就会启动。

Twitter有Twitter名称(也称为screen_name)和twitter id:它们不一样。

//Checking If the app is installed, according to the package name
        Intent intent = new Intent();
        intent.setType("text/plain");
        intent.setAction(Intent.ACTION_SEND);
        final PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

        for (ResolveInfo resolveInfo : list) 
        {
            String packageName = resolveInfo.activityInfo.packageName;

            //In case that the app is installed, lunch it.
            if (packageName != null && packageName.equals("com.twitter.android")) 
            {
                try
                {
                    String formattedTwitterAddress = "twitter://user/" ;
                    Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress));
                                    long twitterId = <Here is the place for the twitter id>
                    browseTwitter.putExtra("user_id", twitterId);
                    startActivity(browseTwitter);

                    return;
                }
                catch (Exception e) 
                {

                }
            }
        }

        //If it gets here it means that the twitter app is not installed. Therefor, lunch the browser.
        try
        { 
                            String twitterName = <Put the twitter name here>
            String formattedTwitterAddress = "http://twitter.com/" + twitterName;
            Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress)); 
            startActivity(browseTwitter);
        }
        catch (Exception e) 
        {

        }

答案 2 :(得分:5)

自2011年12月9日上一版Twitter应用3.0.0以来,官方Twitter应用支持常规意图机制。您需要做的就是使用浏览器的常规意图,如果它是Twitter有效地址,官方应用程序将被注册为此意图的解析器之一。

...只需复制并粘贴goBeepit dev的评论,因为适用于我,Android要求您在浏览器或Twitter应用中打开,我只是使用它

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/norman784"));
startActivity(intent);

答案 3 :(得分:2)

列出回复ACTION_SEND的所有软件包,并在大多数知名的Twitter客户端上过滤它们。 http://regis.decamps.info/blog/2011/06/intent-to-open-twitter-client-on-android/