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();    }}


更多相关文章

  1. Android、iOS添加子视图方法比较
  2. android 通讯录的相关操作
  3. android2.3 View视图框架源码分析之一:android是如何创建一个vie
  4. android 发送短信 判断号码规则 判断字符数70
  5. android获取通讯录
  6. 画廊视图Gallery
  7. 阅读《Android 从入门到精通》(21)——滚动视图
  8. android 向系统通讯录添加一个联系人信息
  9. Android sqlite约束-视图-检查

随机推荐

  1. Android(安卓)Animation学习笔记
  2. Android(安卓)toolbar与actionbar区别
  3. Android层次化安全架构及核心组件概览
  4. 使用Android(安卓)adb命令来启动Android
  5. Android热修复技术链接收集
  6. Android(安卓)渗透测试学习手册 第二章
  7. 新书内容连载(3):Android(安卓)SDK中常用命
  8. 基于ANDROID的网上订餐系统
  9. android 笔记2
  10. Android百度地图导航的那些坑