启动Twitter应用

Aut*_*Eta 6 twitter android android-intent

可能重复:
Android Intent for Twitter应用程序

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

我怎么能这样做,如果用户的手机中没有推特客户端,它应该进入推特的移动网站.

Mar*_*oek 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;
}
Run Code Online (Sandbox Code Playgroud)

(来源)

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

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

(来源)

  • No..tweetIntetnt无法工作..当我开始这个时,我得到这个.使用蓝牙,Dropbox,Gmail发送完成操作 (2认同)

nhe*_*nn1 5

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

这段代码不是以良好的方式编写的,但确实有效.我的建议是将其更改为不使用这么多资源.

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

Twitter有推特名称(也称为screen_name)和推特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) 
        {

        }
Run Code Online (Sandbox Code Playgroud)


nor*_*784 5

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

...只需复制并粘贴goBeepit dev的评论因为对我有用,android要求你在浏览器或Twitter应用程序中打开,我用它只是这个

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