要想启动微信,首先要判断手机是否装了微信,判断的方法如下:
public static boolean isWeixinAvilible(Context context) {
final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName;
if (pn.equals("com.tencent.mm")) {
return true;
}
}
}
return false;
}
然后在项目中加入此代码即可:
Intent intent = new Intent();
ComponentName cmp=newComponentName("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);
startActivity(intent);
同样,跳转QQ也需要判断:
public static boolean isQQClientAvailable(Context context) {
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> pinfo =packageManager.getInstalledPackages(0);
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName;
if (pn.equals("com.tencent.mobileqq")) {
return true;
}
}
}
return false;
}
几个常用的package有:
新浪微博(编辑界面):
com.sina.weibo
com.sina.weibo.EditActivity
腾讯微博(编辑界面):
com.tencent.WBlog
com.tencent.WBlog.activity.MicroblogInput
微信:
com.tencent.mm
com.tencent.mm.ui.LauncherUI
QQ:
com.tencent.mobileqq
com.tencent.mobileqq.activity.HomeActivity