java手机转安卓联系人,调用系统通讯录选择手机号 Android插件

plus.contacts加载全部联系人,只为选择一个联系人时,联系人较多情况下,速度很慢。搞了个插件来调用系统通讯录,只返回选择的一个联系人。

使用:

plus.nativecontact.pick(function(contact){//成功

alert(contact.numbers[0].value);//所选择联系人的第一个电话号码

}, function(status){//失败

});

//contact为联系人json对象,形式:{"givenName":"张三","numbers":[{"value":"123"},{"value":"456"}]}

添加权限:

"nativecontact":{

"description": "系统通讯录"

}

plugin.js:

document.addEventListener("plusready", function()

{

var B = window.plus.bridge;

var nativecontact =

{

"pick":function(successCallback, errorCallback){

var success = typeof successCallback !== 'function' ? null : function(args) {

successCallback(args);

},

fail = typeof errorCallback !== 'function' ? null : function(code) {

errorCallback(code);

},

callbackID = B.callbackId(success, fail);

return B.exec("nativecontact", "pick", [callbackID]);

}

};

window.plus.nativecontact = nativecontact;

}, true);

NativeContact.java:

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.database.Cursor;

import android.net.Uri;

import android.provider.ContactsContract;

import io.dcloud.DHInterface.AbsMgr;

import io.dcloud.DHInterface.IApp;

import io.dcloud.DHInterface.IFeature;

import io.dcloud.DHInterface.ISysEventListener;

import io.dcloud.DHInterface.ISysEventListener.SysEventType;

import io.dcloud.DHInterface.IWebview;

import io.dcloud.util.JSONUtil;

import io.dcloud.util.JSUtil;

public class NativeContact implements IFeature{

private final static int REQUESTCODE = 1;

@Override

public void dispose(String arg0) {

// TODO Auto-generated method stub

}

@Override

public String execute(final IWebview pWebview, final String action, final String[] pArgs) {

if("pick".equals(action))

{

final IApp _app = pWebview.obtainFrameView().obtainApp();

final String callBackId = pArgs[0];

_app.registerSysEventListener(new ISysEventListener(){

@Override

public boolean onExecute(SysEventType pEventType, Object pArgs) {

Object[] _args = (Object[])pArgs;

int requestCode = (Integer)_args[0];

int resultCode = (Integer)_args[1];

Intent data = (Intent)_args[2];

if(pEventType == SysEventType.OnActivityResult){

_app.unregisterSysEventListener(this, SysEventType.OnActivityResult);

if (requestCode == REQUESTCODE) {

if(resultCode == Activity.RESULT_OK){

String phoneNumber = null;

String resultString = "";

Context context = pWebview.getContext();

Uri contactData = data.getData();

Cursor cursor = context.getContentResolver().query(contactData, null, null, null, null);

cursor.moveToFirst();

String givenName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

resultString += "{\"givenName\":\"" + givenName + "\",\"numbers\":[";

String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

Cursor pCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

null,

ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,

null,

null);

while (pCursor.moveToNext()) {

phoneNumber = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

resultString += "{\"value\":\"" + phoneNumber + "\"},";

}

resultString = resultString.substring(0, resultString.length()-1);

resultString += "]}";

cursor.close();

pCursor.close();

JSUtil.execCallback(pWebview, callBackId, JSONUtil.createJSONObject(resultString), JSUtil.OK, false);

}

}

}

return false;

}

}, SysEventType.OnActivityResult);

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

pWebview.getActivity().startActivityForResult(intent, REQUESTCODE);

}

return null;

}

@Override

public void init(AbsMgr arg0, String arg1) {

// TODO Auto-generated method stub

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值