项目中经常会用到一些获取手机里通讯录的功能,这边对其进行了排序
// 读取通讯录 private void readMail() { mallBuffer.setLength(0); mallBuffer.append("["); // 获取手机联系人:最后一个筛选条件就是按时间排序,可以获得最新的 Cursor cursor = getApplicationContext().getContentResolver().query( Phone.CONTENT_URI, null, null, null, Phone._ID + " desc"); // moveToNext方法返回的是一个boolean类型的数据 if (cursor.getCount() >= 50) { int i = 0; while (cursor.moveToNext()) { if (i != 0 && i % 50 == 0) { //判断每50条上传一次 mallString = mallBuffer.toString().substring(0, mallBuffer.length()); mallString = mallString + "]"; sendPhoneInfo("1", mallString); mallBuffer.setLength(0); mallBuffer.append("["); } else if (i == cursor.getCount() - 1) { mallString = mallBuffer.toString().substring(0, mallBuffer.length()); mallString = mallString + "]"; Log.d("duke", "mallString------" + mallString); sendPhoneInfo("1", mallString); } // 读取通讯录的姓名 String name = cursor.getString(cursor .getColumnIndex(Phone.DISPLAY_NAME)); // 读取通讯录的号码 String number = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); //将查询的每一条变成一个json对象 JSONObject jbJsonObject = new JSONObject(); try { jbJsonObject.put(name, number); //对json对象进行拼接成字符json的字符串 mallBuffer.append(jbJsonObject + ""); i++; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else { while (cursor.moveToNext()) { if (cursor.getCount() > 0) { // 读取通讯录的姓名 String name = cursor.getString(cursor .getColumnIndex(Phone.DISPLAY_NAME)); // 读取通讯录的号码 String number = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); Log.d("duke", "name=" + name + "number=" + number); JSONObject jbJsonObject = new JSONObject(); try { jbJsonObject.put(name, number); mallBuffer.append(jbJsonObject + ""); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } mallString = mallBuffer.toString().substring(0, mallBuffer.length()); mallString = mallString + "]"; Log.d("duke", "mallString%%%%%%%%%%%%%%" + mallString); sendPhoneInfo("1", mallString); } }