Android获取联系人

package com.fyfeng.test;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {
	private static final String tag = MainActivity.class.getSimpleName();

	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		this.mContext = this.getApplicationContext();
		test();
	}

	private void test() {

		this.getContacts();
		Log.d(tag, "=======================================");
		this.GetSimContact("content://icc/adn");
		Log.d(tag, "=======================================");
		this.GetSimContact("content://sim/adn");
	}

	private void getContacts() {
		ContentResolver resolver = mContext.getContentResolver();
		Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, null, null, null, null);
		if (phoneCursor != null) {
			while (phoneCursor.moveToNext()) {
				int nameIndex = phoneCursor.getColumnIndex(Phone.DISPLAY_NAME); // 获取联系人name
				String name = phoneCursor.getString(nameIndex);
				String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER)); // 获取联系人number
				Log.d(tag, "phoneNumber = "+phoneNumber+", name = "+name);
			}
			phoneCursor.close();
		}
	}

	private void GetSimContact(String add) {
		// 读取SIM卡手机号,有两种可能:content://icc/adn与content://sim/adn
		try {
			Uri uri = Uri.parse(add);
			Cursor mCursor = getContentResolver().query(uri, null, null, null, null);
			if (mCursor != null) {
				while (mCursor.moveToNext()) {
					// ContactInfo sci = new ContactInfo();
					// 取得联系人名字
					int nameFieldColumnIndex = mCursor.getColumnIndex("name");
					String contactName = mCursor.getString(nameFieldColumnIndex);
					// 取得电话号码
					int numberFieldColumnIndex = mCursor.getColumnIndex("number");
					String userNumber = mCursor.getString(numberFieldColumnIndex);
					// sci.userNumber = GetNumber(sci.userNumber);
					// sci.isChecked = false;

					Log.d(tag, "userNumber = " + userNumber + ", userName = " + contactName);

				}
				mCursor.close();
			}
		} catch (Exception e) {
			Log.i("eoe", e.toString());
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。