从其他应用打开 Twitter 应用中的页面 - Android
Posted
技术标签:
【中文标题】从其他应用打开 Twitter 应用中的页面 - Android【英文标题】:Open page in Twitter app from other app - Android 【发布时间】:2012-06-21 17:40:33 【问题描述】:我正在寻找某种方法来启动 Twitter 应用程序并从我的应用程序中打开指定页面,而无需 web 视图。 我在这里找到了 Facebook 的解决方案: Opening facebook app on specified profile page
我需要类似的东西。
编辑 我刚刚找到了解决方案:
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]")));
【问题讨论】:
谢谢!我会在这里放更具体的例外:ActivityNotFoundException 并且“twitter://status?user_id=[USER_ID]&status_id=[STATUS_ID]”是可能的 @jbc 当我们点击关注时有任何回调...... 不知道,我帮不了你@NagarjunaReddy :( 希望捕获更具体的异常,在本例中为 ActivityNotFoundException。否则,您可能最终会遇到其他问题(如果您向该块添加更多代码)。 【参考方案1】:根据 fg.radigales 的回答,如果可能的话,这就是我用来启动应用程序的方法,但否则会退回到浏览器:
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);
更新
添加了intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
以解决 twitter 在我的应用程序中打开而不是作为新活动打开的问题。
【讨论】:
给出错误目前无法获取用户 twitter android 查看 fg.radigales 答案 那个链接对我没有帮助......无论如何我从其他地方得到它现在正在工作:)【参考方案2】:这对我有用:twitter://user?user_id=id_num
【讨论】:
2016 年只需使用:startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/PROFILENAME")));
我可以通过您提供的链接获取我的 ID。我使用这个网站:mytwitterid.com
如果你想使用 Intent 会是什么样子?喜欢https://twitter.com/intent?text=hello
?【参考方案3】:
通过 2 个步骤从其他使用 Android 的应用打开 Twitter 应用上的页面:
1.只需粘贴以下代码(在按钮单击或任何您需要的地方)
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"));
要获取 USER_ID,只需输入用户名 https://tweeterid.com/ 并在其中获取 Twitter 用户 ID
参考:https://solutionspirit.com/open-page-twitter-application-android/
希望它会有所帮助:)
【讨论】:
【参考方案4】:我的答案建立在 fg.radigales 和 Harry 得到广泛接受的答案之上。 如果用户安装了 Twitter 但已禁用(例如通过使用 App Quarantine),则此方法将不起作用。 Twitter 应用的 Intent 将被选中,但无法处理它,因为它已被禁用。
代替:
getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));
您可以使用以下内容来决定要做什么:
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"));
【讨论】:
【参考方案5】:试试这个代码 sn-p。它会帮助你。
//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)
【讨论】:
请描述如何更明确地获取“twitter id”【参考方案6】:对我来说,如果你有 Twitter 应用程序或访问网络浏览器,它会打开 Twitter 应用程序:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/"+"USERID"));
startActivity(intent);
【讨论】:
以上是关于从其他应用打开 Twitter 应用中的页面 - Android的主要内容,如果未能解决你的问题,请参考以下文章
如何从 iPhone 中的 twitter 集成中取消并返回应用程序?
如何从 Android 中的电子邮件 URL 打开已安装的应用程序? [复制]
如何使用 iOS 上的原生 Twitter 应用打开 Twitter 推文?