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
}
}
28 个评论
要回复文章请先登录或注册
Marco
霸王硬上车
大金子
passage2011
小牛_ge
闪闪 (作者)
飞apple55
闪闪 (作者)
GG虐MM
闪闪 (作者)