Phonegap安卓如何通过点击Contact获得联系人列表的名字和电话

项目源代码可去我的qq群共享下载250395324

1.引入ContactView.java

package com.tricedesigns;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

public class ContactView extends Plugin {
	private static final int PICK_CONTACT = 1;
	private String callback;

	@Override
	public PluginResult execute(String action, JSONArray args, String callbackId) {
		startContactActivity();
		PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
		mPlugin.setKeepCallback(true);
		this.callback = callbackId;
		return mPlugin;
	}

	public void startContactActivity() {
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
		this.ctx.startActivityForResult((Plugin) this, intent, PICK_CONTACT);
	}

	@Override
	public void onActivityResult(int reqCode, int resultCode, Intent data) {
		String name = null;
		String number = null;
		String email = null;
		switch (reqCode) {
		case (PICK_CONTACT):
			if (resultCode == Activity.RESULT_OK) {
				Uri contactData = data.getData();
				Cursor c = this.ctx.managedQuery(contactData, null, null, null, null);
				if (c.moveToFirst()) {
					String ContactID = c.getString(c
							.getColumnIndex(ContactsContract.Contacts._ID));
					String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

					if (Integer.parseInt(hasPhone) == 1) {
						Cursor phoneCursor = this.ctx.managedQuery(
										ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
										null,
										ContactsContract.CommonDataKinds.Phone.CONTACT_ID
												+ "='" + ContactID + "'", null,
										null);
						while (phoneCursor.moveToNext()) {
							number = phoneCursor
									.getString(phoneCursor
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						}
					}
					// get email address
					Cursor emailCur = this.ctx.managedQuery( 
							ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
							null,
							ContactsContract.CommonDataKinds.Email.CONTACT_ID + "='" + ContactID + "'", null,null); 
						while (emailCur.moveToNext()) { 
						    // This would allow you get several email addresses
					            // if the email addresses were stored in an array
						    email = emailCur.getString(
					                      emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
					 	    //String emailType = emailCur.getString(
					          //            emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
					 	} 
					 	emailCur.close();

					name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
					JSONObject contactObject = new JSONObject();
					try {
						contactObject.put("name", name);
						contactObject.put("phone", number);
						contactObject.put("email", email);
					} catch (JSONException e) {
						e.printStackTrace();
					}

					this.success(new PluginResult(PluginResult.Status.OK,
							contactObject), this.callback);
				}
			}
			break;
		}
	}
}

2.引入ContactView.js文件

var ContactView = function() {};

ContactView.prototype.show = function(successCallback, failCallback) {

    function success(args) {
        successCallback(args);
    }
    
    function fail(args) {
    	failCallback(args);
    }

	return cordova.exec(function(args) {
		success(args);
	}, function(args) {
		fail(args);
	}, 'ContactView', '', []);
};

cordova.addConstructor(function() {
	cordova.addPlugin('contactView', new ContactView());
	PluginManager.addService("ContactView","com.tricedesigns.ContactView");
});

3.修改plugins.xml 添加标签:

<plugin name="ContactView" value="com.tricedesigns.ContactView"/>

4.调用方法:

function ccc(){
    	 window.plugins.contactView.show(
    		    	function(contact) {
    		    		alert(contact.name+"|"+contact.phone);
    		    		//document.getElementById("contact-name-from-native").value = contact.name;
    		    		//document.getElementById("contact-phone").value = contact.phone;
    		    	},
    		    	function(fail) {
    		    		alert("We were unable to get the contact you selected.");
    		    	}
    		    );
    }

5.大功告成~~

如图:



点击彩铃后:




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值