您的位置:首页 > 移动开发 > 微信开发

Android 打开外部应用(微博/微信/QQ等)

2016-04-29 10:28 344 查看

跳转微信首页

Intent intent = new Intent();
ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(cmp);
startActivityForResult(intent, 0);


通过以上代码就可跳转到微信首页。

错误处理

在跳转微信的时候,我们还必须要考虑一个问题,那就是:

用户可能在手机中没有安装微信

此时,当我们运行以上的代码,应用就会报
class not found
的错误。

解决方法:

在跳转模块外加一个 try…catch 模块,捕获异常,在catch模块中进行错误处理。

try {
Intent intent = new Intent(); ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI"); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setComponent(cmp); startActivityForResult(intent, 0);
} catch (Exception e) {
//若无法正常跳转,在此进行错误处理
Toast.makeText(MainActivity.this, "无法跳转到微信,请检查您是否安装了微信!", Toast.LENGTH_SHORT).show();
}


其他外部应用

跳转其他应用页面,和跳转微信首页类似。只需将 ComponentName 中的 Package Name(包名) 和 Class Name(类名)相应的替换掉就可以了。

ComponentName("Package Name","Class Name");


常用的Package命令:

ApplicationPackage NameClass Name
新浪微博(编辑界面)com.sina.weibocom.sina.weibo.EditActivity
腾讯微博(编辑界面)com.tencent.WBlogcom.tencent.WBlog.activity.MicroblogInput
微信com.tencent.mmcom.tencent.mm.ui.LauncherUI
QQcom.tencent.mobileqqcom.tencent.mobileqq.activity.HomeActivity
参考文章:

http://www.cnblogs.com/crane13/p/3715203
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: