Open page in Twitter app from other app - Android

2019-01-30 02:12发布

I was looking for some way to launch Twitter app and open a specified page from my application, without webview. I found the solution for Facebook here: Opening facebook app on specified profile page

I need something similar.

EDIT I just found a solution:

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

6条回答
甜甜的少女心
2楼-- · 2019-01-30 02:23

This worked for me: twitter://user?user_id=id_num

To know the ID: http://www.idfromuser.com/

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-30 02:32
const latitude = "40.7127753";
const longitude = "-74.0059728";
const label = "New York, NY, USA";

const url = Platform.select({
  ios: "maps:" + latitude + "," + longitude + "?q=" + label,
  android: "geo:" + latitude + "," + longitude + "?q=" + label
});
Linking.openURL(url);

or with checking if there's a google map app on device and if not open the location in browser

const latitude = "40.7127753";
const longitude = "-74.0059728";
const label = "New York, NY, USA";

const url = Platform.select({
  ios: "maps:" + latitude + "," + longitude + "?q=" + label,
  android: "geo:" + latitude + "," + longitude + "?q=" + label
});

Linking.canOpenURL(url).then(supported => {
  if (supported) {
    return Linking.openURL(url);
  } else {
    browser_url =
      "https://www.google.de/maps/@" +
      latitude +
      "," +
      longitude +
      "?q=" +
      label;
    return Linking.openURL(browser_url);
  }
});
查看更多
戒情不戒烟
4楼-- · 2019-01-30 02:35

Just try this code snippet. It will help you.

//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) 
        {

        }
查看更多
▲ chillily
5楼-- · 2019-01-30 02:42

My answer builds on top of the widely-accepted answers from fg.radigales and Harry. If the user has Twitter installed but disabled (for example by using App Quarantine), this method will not work. The intent for the Twitter app will be selected but it will not be able to process it as it is disabled.

Instead of:

getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));

You can use the following to decide what to do:

PackageInfo info = getPackageManager().getPackageInfo("com.twitter.android", 0);
if(info.applicationInfo.enabled)
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));
else
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/wrkoutapp"));
查看更多
老娘就宠你
6楼-- · 2019-01-30 02:47

Based on fg.radigales answer, this is what I used to launch the app if possible, but fall back to the browser otherwise:

Intent intent = null;
try {
    // get the Twitter app if possible
    this.getPackageManager().getPackageInfo("com.twitter.android", 0);
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USERID"));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch (Exception e) {
    // no Twitter app, revert to browser
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/PROFILENAME"));
}
this.startActivity(intent);

UPDATE

Added intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); to fix an issue where twitter was opening inside my app instead of as a new activity.

查看更多
孤傲高冷的网名
7楼-- · 2019-01-30 02:47

Open page on Twitter app from other app using Android in 2 Steps:

1.Just paste the below code (on button click or anywhere you need)

Intent intent = null;
try{
   // Get Twitter app
   this.getPackageManager().getPackageInfo("com.twitter.android", 0);
   intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USER_ID"));
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch () {
   // If no Twitter app found, open on browser
   intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/USERNAME"));
}

2.intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USER_ID"));

To get USER_ID just write username http://gettwitterid.com/ and get Twitter User ID in there

Reference: https://solutionspirit.com/open-page-twitter-application-android/

Hope it will Help :)

查看更多
登录 后发表回答