2014-01-20 133 views
2

我正在使用Edittext和一個按鈕。在按下按鈕時,電話簿被打開,然後用戶將從中選擇一個聯繫人,所選的電話號碼將顯示在編輯文本上。 我跟着很多教程,但他們顯示的方法已經摺舊了。如何訪問電話簿聯繫人?

我已經聲明此權限:在清單

READ_CONTACTS由Digvesh帕特爾提供

回答

2
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.contact_picker); 

     // this opens the activity. note the Intent.ACTION_GET_CONTENT 
     // and the intent.setType 
     ((Button)findViewById(R.id.pick_person)).setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser 
       Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
       // BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE 
       intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
       startActivityForResult(intent, 1);     
      } 
     }); 
    } 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (data != null) { 
     Uri uri = data.getData(); 

     if (uri != null) { 
      Cursor c = null; 
      try { 
       c = getContentResolver().query(uri, new String[]{ 
          ContactsContract.CommonDataKinds.Phone.NUMBER, 
          ContactsContract.CommonDataKinds.Phone.TYPE }, 
         null, null, null); 

       if (c != null && c.moveToFirst()) { 
        String number = c.getString(0); 
        int type = c.getInt(1); 
        showSelectedNumber(type, number); 
       } 
      } finally { 
       if (c != null) { 
        c.close(); 
       } 
      } 
     } 
    } 
} 

public void showSelectedNumber(int type, String number) { 
    Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();  
} 
+0

我一定要申報比READ_CONTACTS –

+0

以外的任何其他權限是隻所需的權限 –

+0

男人你震撼這傢伙。五個豎起大拇指ü –

0

答案是正確的。他使用「類型」的聯繫人,返回號碼。所以我使用了他的代碼,並對我的應用程序進行了一些更改。它也許有助於有人

public int REQUESTCODE=1; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button select = (Button) findViewById(R.id.select); 
    select.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
      intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
      startActivityForResult(intent, REQUESTCODE); 

     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (data != null) { 
     Uri uri = data.getData(); 
     Log.i("data", uri.toString()); 
     if (uri != null) { 
      Cursor c = null; 
      try { 
       c = getContentResolver().query(uri, new String[]{ 
         ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
          ContactsContract.CommonDataKinds.Phone.NUMBER, 
          ContactsContract.CommonDataKinds.Phone.TYPE }, 
         null, null, null); 

       if (c != null && c.moveToFirst()) { 
        String name = c.getString(0); 
        String number = c.getString(1); 
        int type = c.getInt(2); 

        showSelectedNumber(name, number,type); 
       } 
      } finally { 
       if (c != null) { 
        c.close(); 
       } 
      } 
     } 
    } 
} 

public void showSelectedNumber(String name, String number, int type){ 
    TextView usernumber = (TextView) findViewById(R.id.textView1); 
    String typelabel = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(getResources(), type, ""); 
    usernumber.setText(name+": "+number+" "+typelabel); 

} 
相關問題