16

ボタンをクリックするだけで電話帳に連絡先のリストを表示し、そこから連絡先の1つを選択して、その連絡先番号を取得したいですか?カスタムリストを作成したくないのですが、組み込みの機能を備えたandroidを使用する方法はありますか?

4

7 に答える 7

19

これを試してください-->

   setContentView(R.layout.main);

   contactNumber = (TextView)findViewById(R.id.contactnumber);

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

   @Override
    public void onClick(View arg0) {
   // TODO Auto-generated method stub


   Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
   intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
   startActivityForResult(intent, 1);             


    }});
   }

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

   if(requestCode == RQS_PICK_CONTACT){
   if(resultCode == RESULT_OK){
    Uri contactData = data.getData();
    Cursor cursor =  managedQuery(contactData, null, null, null, null);
    cursor.moveToFirst();

      String number =       cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

      //contactName.setText(name);
      contactNumber.setText(number);
      //contactEmail.setText(email);
     }
     }
     }
     }

EDIT XML が追加されました。

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical" >

      <Button
        android:id="@+id/pickcontact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Pick Contact" />

      <TextView
       android:id="@+id/contactnumber"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

 </LinearLayout>
于 2012-08-25T16:11:41.820 に答える
3
Intent i=new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

startActivityForResult(i, PICK_REQUEST);

メソッドにIntent配信される には、選択した連絡先の が含まれます。これを呼び出すと、これを取得できます。onActivityResult()UrigetData()Intent

これを示すサンプル プロジェクトを次に示します。ロジックは保持されたフラグメントに実装されているため、構成の変更 (ユーザーが画面を回転させるなど) の間、選択された連絡先を保持します。

これにも使用できますACTION_GET_CONTENT。これはより現代的なパターンだと思いますが、ACTION_PICK確かに機能し、この記事の執筆時点で私が持っているサンプル コードはこれだけです。将来これを読んでいる場合 (こんにちは、未来!)、リンク先のサンプルが更新されて を使用する可能性がありますACTION_GET_CONTENT

于 2012-08-25T15:39:15.487 に答える
0

これをテストしたnexus 5xエミュレーターで:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
   intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
   startActivityForResult(intent, 1);   

また:

Uri uri = Uri.parse("content://contacts");
        Intent intent = new Intent(Intent.ACTION_PICK, uri);
        intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        startActivityForResult(intent, 1);

すべての連絡先で機能したわけではありません。どうしてか分かりません。でもこれは:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);

動作します。

于 2016-11-23T13:49:25.140 に答える