码迷,mamicode.com
首页 > 移动开发 > 详细

Android获取联系人

时间:2014-10-30 02:10:29      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:android   io   os   ar   java   sp   on   log   cti   

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



Android获取联系人

标签:android   io   os   ar   java   sp   on   log   cti   

原文地址:http://my.oschina.net/yzw/blog/338666

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!