Open Twitter app from other app and load some page

不羁岁月 提交于 2020-01-13 08:49:09

问题


Is there a way to open the Twitter app from my own application?

For example, I have my own Android app and I want to open Twitter app using Intent. How can I do that? Answers with example would be much appreciated.


回答1:


If the user already has Twitter installed on their phone, something like this should take care of it:

    try{
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_TEXT, "this is a tweet");
            intent.setType("text/plain");
            final PackageManager pm = getPackageManager();
            final List<?> activityList = pm.queryIntentActivities(intent, 0);
            int len =  activityList.size();
            for (int i = 0; i < len; i++) {
                final ResolveInfo app = (ResolveInfo) activityList.get(i);
                if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
                    final ActivityInfo activity=app.activityInfo;
                    final ComponentName name=new ComponentName(activity.applicationInfo.packageName, activity.name);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    intent.setComponent(name);
                    startActivity(intent);
                    break;
                }
            }
      }
        catch(final ActivityNotFoundException e) {
            Log.i("twitter", "no twitter native",e );
        }



回答2:


try {
   startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=" + twitter_user_name)));
}catch (Exception e) {
   startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/#!/" + twitter_user_name)));
}

This should do the necessary work for you.




回答3:


Expanding on @Chrishan's answer you can open the twitter app to perform different functions based on the uri (list below from this post)

twitter://user?screen_name=lorenb
twitter://user?id=12345
twitter://status?id=12345
twitter://timeline
twitter://mentions
twitter://messages
twitter://list?screen_name=lorenb&slug=abcd
twitter://post?message=hello world
twitter://post?message=hello world&in_reply_to_status_id=12345
twitter://search?query=%23hashtag

e.g.

String uriStr = "twitter://post?message=hello world"
try {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(uriStr)));
}catch (Exception e) {
   //the user doesn't have twitter installed
}

NOTE: I have only tried user and post, so if you come across any that don't work please let me know and I will update this answer.




回答4:


I use this:

Intent i = getOpenTwitterIntent(this, "UserName");
startActivity(i);

And the function:

public static Intent getOpenTwitterIntent(Context c, String Username) {

        try {
            c.getPackageManager().getPackageInfo("com.twitter.android", 0);
            return new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name="+ Username));
        } catch (Exception e) {
            return new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/#!/" + Username));
        }
    }

If Twitter app is intalled in the device, this open that, else open web browser...




回答5:


private void showTwitter(){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setClassName("com.twitter.android", "com.twitter.android.ProfileActivity");    
        this.startActivity(intent);     
}


来源:https://stackoverflow.com/questions/6901538/open-twitter-app-from-other-app-and-load-some-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!