在浏览器中可以通过JS代码打开QQ并弹出聊天界面,一般作为客服QQ使用。而在移动端腾讯貌似没有公布提供类似API,但是却可以使用schema模式来启动手机QQ。
以下为具体代码:
Android:
- String url="mqqwpa://im/chat?chat_type=wpa&uin=501863587";
- startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
- UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
- NSURL *url = [NSURL URLWithString:@"mqq://im/chat?chat_type=wpa&uin=501863587&version=1&src_type=web"];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- webView.delegate = self;
- [webView loadRequest:request];
- [self.view addSubview:webView];
浏览器:
- <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=592333407&site=qq&menu=yes">click here</a>
在代码中启动qq,微信,新浪微博等应用程序的代码
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);
// 新浪微博(编辑界面):
// 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
测试中,qq不行,微信的可以,还有需要注意的是,如果当前的设备没有安装qq和微信的的话,会爆出ActivityNotFoundException,这个时候我们只要对我们相应的代码块
进行try catch,然后捕获到异常的话,那就说明我们的设备没有安装qq或者微信,我们再catch语句中,直接进行友好性的提示就可以了