首页 > 代码库 > Android获取联系人
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); } }
Android获取联系人
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。