Android獲取聯系人代碼

jopen 10年前發布 | 24K 次閱讀 Android Android開發 移動開發

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

}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!