android 怎么通过js调用手机通讯录

JavaScript015

android 怎么通过js调用手机通讯录,第1张

思路:

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()")

首先使用手机登录企业微信,你可以用微信号登录也能使用手机登录

2/3

输入你的邮箱帐号来绑定企业,这个邮箱必须是你在企业绑定的邮箱,然后就可以进入你的公司页面了。

3/3

入到企业界面后,点击右上角的同步通讯录,然后就可以同步微信通讯录到你的企业微信了。

http://www.sgpx.com.cn/ksgl/ewebeditor/UploadFile/200841202223965.rar

-------------------

<!--#include file="connection.asp" -->

<!--#include file="tools.asp" -->

<% if request("editid")<>"" then %>

<!--# include file="checkpass.asp" -->

<% end if %>

<%

table="test"

if request.form("addedit_subed")="true" then

'表单提交后

editid=request.form("editid")

xuehao=Request.Form("xuehao")

name=Request.Form("name")

'服务器端验证

'outcheck(CheckValue(xuehao,,,1,"学号"))

'outcheck(CheckValue(name,,,1,"姓名"))

if editid="" then '添加状态

' 防止二义性信息逻辑****************

'set rs_test=Server.CreateObject("ADODB.RECORDSET")

'rs_test.Open "select count(*) as counter from " &table &" where username='" &username &"'",conn

'if rs_test("counter")>0 then

' call Msgout("用户名已存在, 不能添加!","javascript:history.back()",3)

' Response.End

'end if

'************************************

'添加时使用的字段

field1=array("xuehao","name",)

value1=array(xuehao,name,)

for i=0 to ubound(field1)

value1(i)=replace(value1(i),"'","''")

next

sql = "INSERT INTO " &table &" (" &Join(field1, ",") &") VALUES ('" &Join(value1, "','") &"')"

word="你的信息已成功加入!"

else '编辑状态

'编辑时使用的字段

field1=array("xuehao","name",)

value1=array(xuehao,name,)

sql="UPDATE " &table &" SET "

for i=0 to ubound(field1)

sql = sql &field1(i) &"='" &replace(value1(i),"'","''") &"'"

if i <>ubound(field1) then

sql=sql &","

else

sql=sql &" where id=" &editid

end if

next

word="你的信息已成功修改!"

end if

conn.Execute sql

else

'进入页面时(非提交)

editid=request("editid")

if editid<>"" then '编辑状态

onload="onload=""page_onload()"""

set rs1=Server.CreateObject("ADODB.RECORDSET")

rs1.Open "select * from " &table &" where id =" &editid ,conn

id=rs1("id")

xuehao=rs1("xuehao")

name=rs1("name")

'二次密码的值要等于密码字段的值

'dianhua=rs1("password")

else '添加状态

xuehao=""

name=""

end if

end if

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">

<link rel="stylesheet" href="style.css">

<title></title>

<% if request.form("addedit_subed")<>"true" then '********************* %>

<script language="JavaScript" src="check.js"></script>

<script LANGUAGE="javascript">

<!--

<% if editid<>"" then %>

function page_onload(){

//设置单选框、多选框、下拉菜单的值

}

<% end if %>

function add_onsubmit(add) {

/* 如需验证二次密码时用

if(add.password.value!=add.repassword.value){

alert('两次密码不相同!')

return false

}

*/

//客户端验证

//if (!checkvalue(add.xuehao,0,0,1,"学号")) return false

//if (!checkvalue(add.name,0,0,1,"姓名")) return false

}

//-->

</SCRIPT>

<% end if '************************************************* %>

</head>

<body bgcolor="#FFFFFF" <% =onload %>>

<!--#include file="top.inc"-->

<% if request.form("addedit_subed")="true" then %>

<center><% =word %></center>

<% else %>

<form method=POST name=add LANGUAGE=javascript onsubmit="return add_onsubmit(this)">

<input type=hidden name="addedit_subed" value="true">

<input type=hidden name=editid value="<% =editid %>">

<div align=center>

<table border=0 cellpadding=0 width=435 cellspacing=1>

<tr><td width=208 align=right bgcolor=#D2D8BE><span class=fstyle>学号:</span></td>

<td width=359><input type=text name="xuehao" size=20 class=fstyle value="<% =Server.HTMLEncode(xuehao) %>">

</td></tr><tr><td width=208 align=right bgcolor=#D2D8BE><span class=fstyle>姓名:</span></td>

<td width=359><input type=text name="name" size=20 class=fstyle value="<% =Server.HTMLEncode(name) %>">

</td></tr><tr><td width=208 align=right bgcolor=#D2D8BE><span class=fstyle>电话号码:</span></td>

<td width=359><input type=password name="dianhua" size=20 class=fstyle value="<% =Server.HTMLEncode(dianhua) %>">

</td></tr>

</table>

</div>

<div align=center>

<input type=submit value="提交" name=B1 class=fstyle>

<input type=reset value="重填" name=B2 class=fstyle>

</div>

</form>

<% end if %>

<!--#include file="bottom.inc"-->

</body>

</html>