
思路:
1、js调用android的java代码
2、通过java代码访问通讯录
3、通过java代码访问js代码返回通讯录信息
代码:
1、js调用android的java代码
js代码:
window.ncp.callOnJs2("click")android代码,通过webview打开页面:
mWebview.getSettings().setJavaScriptEnabled(true)mWebView.addJavascriptInterface(new JavaScriptInterface(), "ncp")
final class JavaScriptInterface {
public int callOnJs() {
return 1000
}
public void callOnJs2(String mode) {
//TODO
}
}
2、通过java代码访问通讯录
/**得到手机通讯录联系人信息**/private void getPhoneContacts() {
ContentResolver resolver = mContext.getContentResolver()
// 获取手机联系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null)
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
//得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX)
//当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue
//得到联系人名称
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX)
//得到联系人ID
Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX)
//得到联系人头像ID
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX)
//得到联系人头像Bitamp
Bitmap contactPhoto = null
//photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if(photoid > 0 ) {
Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid)
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri)
contactPhoto = BitmapFactory.decodeStream(input)
}else {
contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo)
}
mContactsName.add(contactName)
mContactsNumber.add(phoneNumber)
mContactsPhonto.add(contactPhoto)
}
phoneCursor.close()
}
3、通过java代码访问js代码返回通讯录信息
mWebView.loadUrl("javascript:onSaveCallback()")不可以,虽然说获取对象里面的属性点的方法跟中括号[]都可以,但是两者还是有区别的。
你这里的prop是变量,中括号法可以用变量作为属性名,而点方法不可以。
具体你参考下这个网站网页链接
1 <!DOCTYPE html>2 <html>
3 <head>
4 <title>微信WeixinJSBridge API</title>
5 <meta charset="utf-8" />
6 <script type="text/javascript">
7 (function(){
8 var a=document.getElementsByTagName("html")[0]
9 window.Session={appDomain:a.getAttribute("data-app-domain")||"",staticDomain:a.getAttribute("data-static-domain")||""}
10 })()
11
12 window.registNS=function(fullNS,isIgnorSelf){
13 var reg=/^[_$a-z]+[_$a-z0-9]*/ivar nsArray=fullNS.split(".")
14 var sEval=""
15 var sNS=""
16 var n=isIgnorSelf?nsArray.length-1:nsArray.length
17 for(var i=0i<ni++){
18 if(!reg.test(nsArray[i])){throw new Error("Invalid namespace:"+nsArray[i]+"")
19 return
20 }
21 if(i!=0){sNS+="."}
22 sNS+=nsArray[i]
23 sEval+="if(typeof("+sNS+")=='undefined') "+sNS+"=new Object()
24 else "+sNS+""
25 }
26 if(sEval!=""){
27 return eval(sEval)
28 }
29 return{}
30
31 }
32
33 </script>
34 </head>
35 <body>
36 <section class="mod-page-body">
37<div class="mod-page-main wordwrap clearfix">
38 <div class="mod-pageheader"></div>
39 <div class="mod-pagecontent">
40 <div class="mod-weixinjsapi">
41 <div class="x-desc">
42微信客户端自带的Js Api:WeixinJSBridge
43 </div>
44 <div id="WeixinJsApi">
45<input type="button" id="imagePreview" value="图片预览" />
46<input type="button" id="profile" value="查看profile" />
47<a href="weixin://profile/gh_412d74fbb474">企业微信小助手</a>
48<input type="button" id="shareWeibo" value="分享微博" />
49<input type="button" id="shareFB" value="分享facebook" />
50<input type="button" id="addContact" value="添加联系人" />
51<input type="button" id="scanQRCode" value="扫描二维码" />
52<input type="button" id="jumpToBizProfile" value="跳转到指定公众账号页面" />
53<input type="button" id="toggleMenuBtn" value="隐藏右上角按钮" />
54<input type="button" id="toggleToolbar" value="隐藏底部导航栏" />
55<input type="button" id="getNetType" value="获取网络状态" />
56<input type="button" id="closeWindow" value="关闭" />
57<input type="button" id="getBrandWCPayRequest" value="发起公众号微信支付" />
58<input type="button" id="setPageState" value="设置页面状态" />
59<input type="button" id="sendEmail" value="发邮件" />
60<input type="button" id="openSpecificView" value="微信团队打开webView,跳到指定页面" />
61<input type="button" id="getCanIAPPay" value="getCanIAPPay" />
62<input type="button" id="getBrandIAPPayRequest" value="发起公众号IAP支付" />
63<input type="button" id="openUrlByExtBrowser" value="用safari打开指定链接" />
64<input type="button" id="openProductView" value="跳转微信商品页" />
65<input type="button" id="openLocation" value="查看地理位置" />
66<input type="button" id="timelineCheckIn" value="朋友圈签到" />
67<input type="button" id="getBrandWCPayCreateCreditCardRequest" value="开通微信xyk" />
68<input type="button" id="geoLocation" value="获取地理位置" />
69<input type="button" id="getInstallState" value="获取某app是否安装" />
70<input type="button" id="editAddress" value="公众号编辑收货地址" />
71<input type="button" id="getLatestAddress" value="公众号获取最近的收货地址" />
72<input type="button" id="launch3rdApp" value="启动第三方APP" />
73<input type="button" id="jumpWCMall" value="跳转微信商品购买界面" />
74<input type="button" id="addEmoticon" value="添加表情" />
75<input type="button" id="cancelAddEmoticon" value="取消下载某表情" />
76<input type="button" id="hasEmoticon" value="查询是否存在某表情" />
77 </div>
78 </div>
79 </div>
80</div>
81 </section>
82 <script>
83 function onBridgeReady() {
84 WeixinJSBridge.on('menu:share:appmessage', function(argv)
85 {
86 WeixinJSBridge.invoke('sendAppMessage',{
87 "link":"http://m.exmail.qq.com/",
88 "desc":"desc",
89 "title":"title for WeiXinJsBridge"
90 }, function(res) {
91 WeixinJSBridge.log(res.err_msg)
92 })
93 })
94 WeixinJSBridge.on('menu:share:timeline', function(argv)
95 {
96 WeixinJSBridge.invoke("shareTimeline",{
97 "link":"http://m.exmail.qq.com",
98 "img_url":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/logo1ca3fe.png",
99 "img_width":"172",
100 "img_height":"40",
101 "desc":"i am description",
102 "title":"just test from WeixinJsBridge"
103 },
104 function(e){
105 alert(e.err_msg)
106 })
107 })
108 }
109
110 if (typeof WeixinJSBridge === "undefined"){
111 if (document.addEventListener){
112 document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false)
113 }
114 }else{
115 onBridgeReady()
116 }
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)