Android 根据QQ号跳转到QQ聊天界面

时间:2023-03-09 03:45:59
Android 根据QQ号跳转到QQ聊天界面

从自己开发的应用中根据QQ号跳转到QQ应用的聊天界面,实现起来很方便:

即:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin="+qqNum+"&version=1")));  

qqNum(string型)即是所要跳转的qq号。

不过如果直接写这个代码的话,若本机未安装qq应用,程序会直接go die,所以要加以判断:

if (checkApkExist(this, "com.tencent.mobileqq")){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin="+qqNum+"&version=1")));
}else{
Toast.makeText(this,"本机未安装QQ应用",Toast.LENGTH_SHORT).show();
}
break;

方法checkApkExit()如下:

public boolean checkApkExist(Context context, String packageName) {
if (packageName == null || "".equals(packageName))
return false;
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}

注:qq的应用包名是:com.tencent.mobileqq