Android获取手机短信和通话记录及通讯录

android 下不同进程是通过ContentResolver共享数据的,下面通过这个类去查询手机上所有联系人的信息和通话记录,包含号码、内容、日期,通话时长等信息

一、获取手机短信信息

/**
	 * 获取所有短信
	 * 
	 * @return
	 */
	public String getSmsInPhone() {
		final String SMS_URI_ALL = "content://sms/";

		StringBuilder smsBuilder = new StringBuilder();

		try {
			Uri uri = Uri.parse(SMS_URI_ALL);
			String[] projection = new String[] { "_id", "address", "person",
					"body", "date", "type" };
			Cursor cur = getContentResolver().query(uri, projection, null,
					null, "date desc"); // 获取手机内部短信

			if (cur.moveToFirst()) {
				int index_Address = cur.getColumnIndex("address");
				int index_Person = cur.getColumnIndex("person");
				int index_Body = cur.getColumnIndex("body");
				int index_Date = cur.getColumnIndex("date");
				int index_Type = cur.getColumnIndex("type");

				do {
					String strAddress = cur.getString(index_Address);
					int intPerson = cur.getInt(index_Person);
					String strbody = cur.getString(index_Body);
					long longDate = cur.getLong(index_Date);
					int intType = cur.getInt(index_Type);

					SimpleDateFormat dateFormat = new SimpleDateFormat(
							"yyyy-MM-dd hh:mm:ss");
					Date d = new Date(longDate);
					String strDate = dateFormat.format(d);

					String strType = "";
					if (intType == 1) {
						strType = "接收";
					} else if (intType == 2) {
						strType = "发送";
					} else {
						strType = "null";
					}

					smsBuilder.append("[ ");
					smsBuilder.append(strAddress + ", ");
					smsBuilder.append(intPerson + ", ");
					smsBuilder.append(strbody + ", ");
					smsBuilder.append(strDate + ", ");
					smsBuilder.append(strType);
					smsBuilder.append(" ]\n\n");
				} while (cur.moveToNext());

				if (!cur.isClosed()) {
					cur.close();
					cur = null;
				}
			} else {
				smsBuilder.append("no result!");
			} // end if

			smsBuilder.append("getSmsInPhone has executed!");

		} catch (SQLiteException ex) {
			Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
		}

		return smsBuilder.toString();
	}

二、获取通话记录


/**
	 * 获取通话记录
	 */
	private void GetCallsInPhone() {
		String result = null;
		Cursor cursor = getContentResolver().query(
				Calls.CONTENT_URI,
				new String[] { Calls.DURATION, Calls.TYPE, Calls.DATE,
						Calls.NUMBER }, null, null, Calls.DEFAULT_SORT_ORDER);
		boolean hasRecord = cursor.moveToFirst();
		int count = 0;
		String strPhone = "";
		String date;

		while (hasRecord) {
			int type = cursor.getInt(cursor.getColumnIndex(Calls.TYPE));
			long duration = cursor.getLong(cursor
					.getColumnIndex(Calls.DURATION));
			strPhone = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd hh:mm:ss");
			Date d = new Date(Long.parseLong(cursor.getString(cursor
					.getColumnIndex(Calls.DATE))));
			date = dateFormat.format(d);

			result = result + "phone :" + strPhone + ",";

			result = result + "date :" + date + ",";
			result = result + "time :" + duration + ",";

			switch (type) {
			case Calls.INCOMING_TYPE:
				result = result + "type :呼入";
				break;
			case Calls.OUTGOING_TYPE:
				result = result + "type :呼出";
			default:
				break;
			}
			result += "\n";
			count++;
			hasRecord = cursor.moveToNext();
		}
		Log.i(TAG, result);

		textView.setText(result);
	}

三、获取联系人

	/**
	 * 获取联系人
	 */
	private void getContact2() {
		String string = "";
		int count = 0;
		ContentResolver resolver = getApplicationContext().getContentResolver();

		Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI,
				null, null, null, null);

		while (cursor.moveToNext()) {

			// 取得联系人的名字索引
			int nameIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
			String contact = cursor.getString(nameIndex);
			string += contact + ":";
			// 取得联系人的ID索引值
			String contactId = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts._ID));

			// 查询该位联系人的电话号码,类似的可以查询email,photo
			Cursor phone = resolver.query(
					ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
					ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
							+ contactId, null, null);// 第一个参数是确定查询电话号,第三个参数是查询具体某个人的过滤值

			// 一个人可能有几个号码
			while (phone.moveToNext()) {
				String strPhoneNumber = phone
						.getString(phone
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				string = string + strPhoneNumber + " ;";

			}

			string += "\n";
			count++;
			phone.close();
		}
		cursor.close();
		Log.i(TAG, String.valueOf(count));
		// 设置显示内容
		textView.setText(string);


	}
最后需要注意的是要启动访问权限

   
    
    

源码下载地址:http://download.csdn.net/detail/leokelly001/8129689


你可能感兴趣的:(Android)