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;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(); this.getSimContact("content://icc/adn"); this.getSimContact("content://sim/adn"); } private void getContacts() { ContentResolver resolver = mContext.getContentResolver(); Cursor cursor = null; try { cursor = resolver.query(Phone.CONTENT_URI, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { int index_displayName = cursor.getColumnIndex(Phone.DISPLAY_NAME); int index_phoneNumber = cursor.getColumnIndex(Phone.NUMBER); do { String displayName = cursor.getString(index_displayName); String phoneNumber = cursor.getString(index_phoneNumber); Log.d(tag, "phoneNumber = " + phoneNumber + ", displayName = " + displayName); } while (cursor.moveToNext()); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != cursor) { cursor.close(); } } } /** * 读取SIM卡手机号,有两种可能:content://icc/adn与content://sim/adn * * @param uriString */ private void getSimContact(String uriString) { // Cursor cursor = null; try { Uri uri = Uri.parse(uriString); cursor = getContentResolver().query(uri, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { int index_name = cursor.getColumnIndex("name"); int index_number = cursor.getColumnIndex("number"); do { String name = cursor.getString(index_name); String number = cursor.getString(index_number); Log.d(tag, "number = " + number + ", name = " + name); } while (cursor.moveToNext()); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != cursor) { cursor.close(); } } }}