Android 从其他应用打开Twitter应用并加载一些页面
有没有办法从我自己的应用程序打开Twitter应用程序 例如,我有自己的Android应用程序,我想使用Intent打开Twitter应用程序。 我该怎么做?Android 从其他应用打开Twitter应用并加载一些页面,android,twitter,Android,Twitter,有没有办法从我自己的应用程序打开Twitter应用程序 例如,我有自己的Android应用程序,我想使用Intent打开Twitter应用程序。 我该怎么做? 如果用户的手机上已经安装了Twitter,那么像这样的东西应该会很好地解决这个问题: try{ Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, "this is a
如果用户的手机上已经安装了Twitter,那么像这样的东西应该会很好地解决这个问题:
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 );
}
试试看{
意向意向=新意向(意向.行动\发送);
intent.putExtra(intent.EXTRA_文本,“这是一条推特”);
intent.setType(“文本/普通”);
最终PackageManager pm=getPackageManager();
最终列表activityList=pm.querytentActivities(intent,0);
int len=activityList.size();
对于(int i=0;i
这将为您完成必要的工作。我使用以下方法:
Intent i = getOpenTwitterIntent(this, "UserName");
startActivity(i);
以及功能:
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));
}
}
如果在设备中安装了Twitter应用程序,则打开该应用程序,否则打开web浏览器…在@Chrishan的答案上展开,您可以打开Twitter应用程序,根据uri执行不同的功能(如下所示) e、 g
注意:我只尝试了
用户
和发布
,因此如果您遇到任何不起作用的问题,请告诉我,我将更新此答案。@Adam Storm,我应该声明,只有当用户的手机上安装了Twitter时,此功能才起作用。安装应用程序时,您的答案很有效,但当它打开浏览器时,它不是重定向到帐户。如果应用程序未安装,您可以帮助使用此链接startActivity(新的意图(Intent.ACTION\u视图,Uri.parse(“+twitter\u用户名”);没有#!为我工作
Intent i = getOpenTwitterIntent(this, "UserName");
startActivity(i);
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));
}
}
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
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
}