2010-12-01 37 views
0

我有一個應用程序必須顯示我的手機聯繫人列表。用戶必須選擇一個電話號碼,我必須在我的應用程序中以編程方式使用此電話號碼。 我該怎麼辦?如何顯示我的聯繫人電話號碼並獲取其中一個電話號碼以便在我的應用中使用?

代碼示例會很好。

+0

問題不清楚,你現在究竟想要什麼? – schwiz 2010-12-01 20:51:58

+0

可能的重複問題http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list – schwiz 2010-12-01 19:33:37

回答

3

剛絲了一個按鈕,onBrowseForNumbersButtonClicked()方法......在formattedPhoneNumber線下方放下你的代碼......你是好去。

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.provider.ContactsContract.CommonDataKinds.Phone; 
import android.util.Log; 
import android.view.View; 

public class TestActivity extends Activity { 

    private static final int REQUEST_CONTACT_NUMBER = 123456789; 

    /** Pops the "select phone number" window */ 
    public void onBrowseForNumbersButtonClicked(View view) { 
     Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI); 
     startActivityForResult(contactPickerIntent, REQUEST_CONTACT_NUMBER); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if(data != null && requestCode == REQUEST_CONTACT_NUMBER) { 
       Uri uriOfPhoneNumberRecord = data.getData(); 
       String idOfPhoneRecord = uriOfPhoneNumberRecord.getLastPathSegment(); 
       Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.NUMBER}, Phone._ID + "=?", new String[]{idOfPhoneRecord}, null); 
       if(cursor != null) { 
         if(cursor.getCount() > 0) { 
          cursor.moveToFirst(); 
          String formattedPhoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER)); 
          Log.d("TestActivity", String.format("The selected phone number is: %s", formattedPhoneNumber)); 
         } 
         cursor.close(); 
       } 
      } 
      else { 
       Log.w("TestActivity", "WARNING: Corrupted request response"); 
      } 
     } 
     else if (resultCode == RESULT_CANCELED) { 
      Log.i("TestActivity", "Popup canceled by user."); 
     } 
     else { 
      Log.w("TestActivity", "WARNING: Unknown resultCode"); 
     } 
    } 
} 
相關問題