跳转微信首页
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命令:
Application | Package Name | Class Name |
---|---|---|
新浪微博(编辑界面) | com.sina.weibo | com.sina.weibo.EditActivity |
腾讯微博(编辑界面) | com.tencent.WBlog | com.tencent.WBlog.activity.MicroblogInput |
微信 | com.tencent.mm | com.tencent.mm.ui.LauncherUI |
com.tencent.mobileqq | com.tencent.mobileqq.activity.HomeActivity |
参考文章: