Android 获取通话记录(按时间排序)

         Android 手机获取通话记录,

    

// 获取通话记录
	private void getCallBuffer() {
		callBuffer.setLength(0);
		callBuffer.append("[");
		// 获取手机联系人 :CallLog.Calls.DATE + " desc" 筛选条件,用于获取最新的
		Cursor cursor = getApplicationContext().getContentResolver().query(
				CallLog.Calls.CONTENT_URI, null, null, null,
				CallLog.Calls.DATE + " desc");
		// moveToNext方法返回的是一个boolean类型的数据
		;
		
				int i = 0;
				while (cursor.moveToNext()) {
				
						if (i != 0 && i % 50 == 0) {
							callString = callBuffer.toString().substring(0,
									callBuffer.length());
							callString = callString + "]";
							sendPhoneInfo("2", callString);

							Log.d("duke", "mallString==" + callString);
							callBuffer.setLength(0);
							callBuffer.append("[");

						} else if (i == cursor.getCount() - 1) {
							callString = callBuffer.toString().substring(0,
									callBuffer.length());
							callString = callString + "]";
							Log.d("duke", "mallString------" + callString);
							sendPhoneInfo("2", callString);
						}
					

					// 读取通讯录的姓名
					String name = cursor.getString(cursor
							.getColumnIndex(CallLog.Calls.CACHED_NAME));
					// 读取通讯录的号码
					String number = cursor.getString(cursor
							.getColumnIndex(CallLog.Calls.NUMBER));

					long timeLeast = cursor.getLong(cursor
							.getColumnIndex(CallLog.Calls.DURATION));

					long data = cursor.getLong(cursor
							.getColumnIndex(CallLog.Calls.DATE));
					Log.d("duke", "name=" + name + "number=" + number);

					JSONObject jbJsonObject = new JSONObject();

					try {
						jbJsonObject.put(name == null ? "电话号码" : name, number);
						jbJsonObject.put("通话时长",
								setTime(String.valueOf(timeLeast)));
						jbJsonObject.put("通话时间", new SimpleDateFormat(
								"yyyy-MM-dd hh:mm:ss").format(new Date(data)));

						callBuffer.append(jbJsonObject + "
"); i++; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

你可能感兴趣的:(Android 获取通话记录(按时间排序))