Whatsapp不像Facebook, 有自己的API, 在cocos android游戏分享到whatsapp只能用android最原始的Intent方式 ,但是一般分享只能分享出纯文本,即
sendIntent.putExtra(Intent.EXTRA_TEXT,"XX") 这样的形式,其他的EXTRA_字段统统无效,如
Intent.EXTRA_TITLE,Intent.EXTRA_EXTRA_SUBJECT,Intent.EXTRA_HTML_TEXT统统无效,设了也是白设,网上一大堆分享超链接的方法,如 sendIntent.putExtra(Intent.EXTRA_HTML_TEXT, Html.fromHtml("一段网页格式的文本"));也是无效,直接报错无法分享空白内容,因为Html.fromHtml返回的是android.text.Spanned, 不是文本。经过我不断的搜索google, 搜索stackoverflow,终于有了解决方法,代码如下:
String shareUrlStr = "https://wa.me/?text=I'm inquiring about the apartment listing http://www.baidu.com/landing/index.html?ic=44433&source=10015";
//shareUrlStr = "https://wa.me/?text=I'm inquiring about the apartment listing http://175.178.103.49/landing/index.html/ic=4433&source=10015";
Uri uriUrl = Uri.parse(shareMessage);
Intent launchBrowerIntent = new Intent(Intent.ACTION_VIEW, uriUrl);
//launchBrowerIntent.setAction(Intent.ACTION_SEND);
//Target whatsapp
// launchBrowerIntent.setPackage("com.whatsapp");
mActivity.startActivity(Intent.createChooser(launchBrowerIntent, "Send to friend"));
要点是网址的文本前要加上"https://wa.me/?text=" + 你想要的文本,带网址链接,再用Uri.parse解析一下就行了,但是要注意这个网址不能是IP地址开头形式,如我注掉的http://175.178.103.49/index.html?score=1005 这个分享时就转换不了链接,而 http://www.daidai.org/index.html?score=1005这种形式就全文都能显示出超链接的形式,这个要注意。