1、起调QQ聊天
public void toQQ(){// 跳转之前,可以先判断手机是否安装QQif (isQQClientAvailable(this)) {// 跳转到客服的QQString url = "mqqwpa://im/chat?chat_type=wpa&uin=200870769";Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse(url));// 跳转前先判断Uri是否存在,如果打开一个不存在的Uri,App可能会崩溃if (isValidIntent(this,intent2)) {startActivity(intent2);}else {Toast.makeText(MainActivity.this,"客服QQ异常",Toast.LENGTH_SHORT).show();}}else {Toast.makeText(MainActivity.this,"请先安装QQ客户端",Toast.LENGTH_SHORT).show();} }
2、工具方法
/*** 判断用户是否安装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.equalsIgnoreCase("com.tencent.qqlite") || pn.equalsIgnoreCase("com.tencent.mobileqq")) {return true;}}}return false;
}
/*** 判断Uri是否有效*/
public static boolean isValidIntent(Context context, Intent intent) {PackageManager packageManager = context.getPackageManager();List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);return !activities.isEmpty();
}