how to access contacts in my android program

后端 未结 4 440
情歌与酒
情歌与酒 2021-01-31 06:58

I\'m making an text message application and want to access contacts in my Android application .

I want to access contacts as if they where in the actual contact list. W

4条回答
  •  时光说笑
    2021-01-31 07:19

    use this code to pick contact:

     Button button = (Button)findViewById(R.id.pickcontact);
    
        button.setOnClickListener(new OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                     Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                     startActivityForResult(intent, PICK_CONTACT);
                 }
             });
    
    
    
        @Override public void onActivityResult(int reqCode, int resultCode, Intent data){ super.onActivityResult(reqCode, resultCode, data);
    
        switch(reqCode)
        {
           case (PICK_CONTACT):
             if (resultCode == Activity.RESULT_OK)
             {
                 Uri contactData = data.getData();
                 Cursor c = managedQuery(contactData, null, null, null, null);
              if (c.moveToFirst())
              {
              String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
    
              String hasPhone =
              c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    
              if (hasPhone.equalsIgnoreCase("1")) 
              {
             Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
              ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                phones.moveToFirst();
                String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
               // Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show();
                setCn(cNumber);
              }
             }
           }
        }
       }
    

提交回复
热议问题