Android 2.1读取手机通讯录

 android2.1读取手机通讯录功能和以前版本不太一样,看代码

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
/**
 * android 2.1以上读取手机通讯录,一个人有多个号码时用|分隔
 * @author acer
 *
 */
public class PhoneActivity extends Activity {
    LinearLayout mLinearLayout;
    ListView mLstView;
    ArrayList<Map<String, String>> listData ;
    
    static final String NAME="name";
    static final String NUMBER="number";
    String jsondata = "";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLinearLayout = new LinearLayout(this);
        mLinearLayout.setOrientation(LinearLayout.VERTICAL);
        mLinearLayout.setBackgroundColor(Color.BLACK);
        
        mLstView = new ListView(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        mLstView.setBackgroundColor(Color.BLACK);
        
        mLinearLayout.addView(mLstView, params);
        setContentView(mLinearLayout);
        
        listData = new ArrayList<Map<String, String>>();
        
        // 读取通讯录
        jsondata = "[";
        String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
        Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, select, null, null);
        startManagingCursor(cur);
        
        while (cur.moveToNext()) {
            JSONObject phonedate = new JSONObject();
            Map<String, String> mpMap = new HashMap<String, String>();
            String _id = cur.getString(cur.getColumnIndex("_id"));
            Cursor pcur =  getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + _id, null, null);
            // 处理多个号码的情况
            String phoneNumbers = "";
            while(pcur.moveToNext()){
                String strPhoneNumberString = pcur.getString(pcur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                phoneNumbers += strPhoneNumberString + "|";
            }
            pcur.close();
            
            String nameString = cur.getString(cur.getColumnIndex("display_name"));
            mpMap.put(NAME, nameString);
            mpMap.put(NUMBER, phoneNumbers);
            listData.add(mpMap);
            try {
                phonedate.put("phone_id", _id);
                phonedate.put("name", nameString);
                phonedate.put("number", phoneNumbers);
            } catch (JSONException e) {
                Log.e("PHONE", "构建JSON数据出现异常", e);
                e.printStackTrace();
            }
        }
        
        cur.close();
        // 建立一个适配器去查询数据
        ListAdapter adapter = new SimpleAdapter(this, listData, android.R.layout.simple_list_item_2, new String[]{NAME,NUMBER}, new int[] {android.R.id.text1,android.R.id.text2});
        mLstView.setAdapter(adapter);
        /* 为m_ListView视图添加setOnItemSelectedListener监听 */
        mLstView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                DisplayToast("滚动到第" + Long.toString(arg0.getSelectedItemId()) + "项");
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
        /* 为m_ListView视图添加setOnItemClickListener监听 */
        mLstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // 于对选中的项进行处理
                DisplayToast("选中了第" + Integer.toString(arg2 + 1) + "项");
            }
        });
    }
    /* 显示Toast */
    public void DisplayToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
读取Android手机通讯录,可以使用Android提供的ContentResolver类和ContactsContract类。下面是一个简单的示例代码: ```java import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.provider.ContactsContract; public class ContactReader { public static void readContacts(Context context) { ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = null; try { cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); if (phoneCursor != null && phoneCursor.moveToNext()) { String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); System.out.println("Name: " + name + ", Phone Number: " + phoneNumber); } if (phoneCursor != null) { phoneCursor.close(); } } } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } } } } ``` 在上面的代码中,我们首先获取ContentResolver对象,然后使用它查询ContactsContract.Contacts.CONTENT_URI来获取所有联系人的ID和名称。接着,我们使用联系人ID查询ContactsContract.CommonDataKinds.Phone.CONTENT_URI来获取联系人的电话号码。最后,我们打印联系人的名称和电话号码。调用readContacts()方法即可读取手机通讯录

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值