Picking the preferred Phonenumer of a System Contact
The following Activity is an example of how to get the preferred phonenumber of a system-contact. It utilizes an Intent with the systems PICK_ACTION ( [font=Century Gothic]android.content.Intent.ACTION_PICK[/font] ).
Using java Syntax Highlighting
- package org.anddev.smstretcher.ui;
- import org.anddev.smstretcher.R;
- import android.app.Activity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.Contacts.People;
- import android.telephony.PhoneNumberFormattingTextWatcher;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.EditText;
- import android.widget.ImageButton;
- public class Compose extends Activity {
- // ===========================================================
- // Constants
- // ===========================================================
- public static final int REQUESTCODE_PICKCONTACT = 0;
- // ===========================================================
- // Fields
- // ===========================================================
- protected EditText mEtContactNumber;
- // ===========================================================
- // Constructors
- // ===========================================================
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.compose);
- initViews();
- }
- private void initViews() {
- this.mEtContactNumber = (EditText)findViewById(R.id.et_compose_targetnumber);
- final ImageButton ibtnPickContact = (ImageButton)this.findViewById(R.id.ibtn_compose_pickcontact);
- ibtnPickContact.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- Intent i = new Intent(android.content.Intent.ACTION_PICK, Uri.parse("content://contacts/people"));
- startActivityForResult(i, REQUESTCODE_PICKCONTACT);
- }
- });
- }
- // ===========================================================
- // Getter & Setter
- // ===========================================================
- // ===========================================================
- // Methods from SuperClass/Interfaces
- // ===========================================================
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- switch(requestCode){
- case REQUESTCODE_PICKCONTACT:
- if(data != null){
- final Uri personUri = data.getData();
- if(personUri != null){
- final Cursor c = getContentResolver().query(personUri, null, null, null, null);
- startManagingCursor(c);
- if(c != null && c.moveToFirst()){
- final long prefferedPhoneID = c.getLong(c.getColumnIndexOrThrow(People.PRIMARY_PHONE_ID));
- final Uri phoneUri = Uri.withAppendedPath(People.CONTENT_URI, "" + prefferedPhoneID).buildUpon().appendPath(People.Phones.CONTENT_DIRECTORY).build();
- final Cursor d = getContentResolver().query(phoneUri, null, null, null, null);
- startManagingCursor(c);
- if(d != null && d.moveToFirst()){
- final String num = d.getString(d.getColumnIndexOrThrow(People.Phones.NUMBER));
- this.mEtContactNumber.setText(num);
- }
- }
- }
- }
- break;
- }
- }
- }
Parsed in 0.048 seconds, using GeSHi 1.0.8.4
Thats it 

Regards,
plusminus




