我想从Android中的联系簿中选择一个号码

时间:2012-05-24 06:42:16

标签: android android-intent android-widget android-contacts

此代码显示联系号码列表,但我想从所选联系人显示名称中选择手机号码--->

        Cursor cursor= managedQuery(intent.getData(), null, null, null, null);
               while(cursor.moveToNext()) {
                String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                System.out.println("---------ContactId---------"+contactId);
               String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
               System.out.println("---------NAME---------"+name);
               String hasPhone=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            System.out.println("---------HAS Phone---------"+hasPhone);
         ArrayList one= new ArrayList();
         ArrayList two= new ArrayList();
   //   if(Boolean.parseBoolean(hasPhone)) {
          Cursor phones=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,          ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = "+ contactId, null, null);
         while(phones.moveToNext()) {
        phoneNumber= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
           System.out.println("---------Number---------"+phoneNumber);
        one.add(phoneNumber);
           System.out.println("---------email Address---------"+one);
         } phones.close();

//}

2 个答案:

答案 0 :(得分:0)

显示名称

public class ContentProviderActivity extends Activity {

ListView lv;
Map<String, List<String>> mymap;


/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lv = (ListView)findViewById(R.id.listContact);
    mymap = new HashMap<String, List<String>>();

    Uri allContacts = Uri.parse("content://contacts/people/");
    Cursor mCursor = managedQuery(allContacts, null, null, null, ContactsContract.Contacts._ID + " ASC");


    final String[] contacts = new String[]{ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts._ID};

    int [] view = new int[]{R.id.txtName,R.id.txtID};

    final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.main, mCursor, contacts, view);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener() {


    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position,
            long id) {
        // TODO Auto-generated method stub

        //displayContacts(position+1);
        int id1 = (int) adapter.getItemId(position);
        Intent i = new Intent(getApplicationContext(),ShowContactNo.class);
        i.putExtra("ID", id1);
        startActivity(i);
     }
});
}


}

ShowContactNo:显示相关联系人号码

public class ShowContactNo extends ListActivity{

Map<String, List<String>> mymap;
String name;
List<String> Phone_No;
String select_Number;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    mymap = new HashMap<String, List<String>>();

    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_NONE);

    Intent i = getIntent();
    int position = i.getIntExtra("ID", 0);
    displayContacts(position);

    Phone_No = new ArrayList<String>();
    Phone_No = mymap.get(name);
    System.out.println(Phone_No);

    if(Phone_No!=null)
    {
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_checked, Phone_No));
    }



     final String [] items          = new String [] {"Make Call", "Send Text SMS"};             

       ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);

       AlertDialog.Builder builder      = new AlertDialog.Builder(this);

        builder.setTitle("Select Option");

        builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
            public void onClick( DialogInterface dialog, int item ) {
                if (item == 0) {
                    Intent i = new
                    Intent(android.content.Intent.ACTION_CALL, 
                            Uri.parse("tel:"+select_Number));
                startActivity(i);

                    dialog.cancel();
                } else {

                    Intent i = new 
                    Intent(android.content.Intent.ACTION_SENDTO, 
                            Uri.parse("smsto:"+select_Number));
                    i.putExtra("sms_body", "Krishnakant Dalal");
                    startActivity(i);
                }
            }
        } );

        final AlertDialog dialog = builder.create();


    listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub

                    select_Number = String.valueOf(Phone_No.get(arg2));
                    dialog.show();
                }
    });
}


private void displayContacts(int position) {
    if(position!=0)
    {
    ContentResolver cr = getContentResolver();
      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
              null, ContactsContract.Contacts._ID +" = ?",
              new String[]{String.valueOf(position)}, null);

      if (cur.getCount() > 0) {
          while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString(
                      cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                   Cursor pCur = cr.query(
                             ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                             null,
                             ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                             new String[]{id}, null);

                   List<String> numberlist = new ArrayList<String>();

                   while (pCur.moveToNext()) {
                       String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                      // Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                       numberlist.add(phoneNo);
                   }
                  pCur.close();

                  mymap.put(name, numberlist);  
              }
          }
      }
    }
  }

}  

别忘了添加权限:

<uses-permission android:name="android.permission.READ_CONTACTS" /> 
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
<uses-permission android:name="android.permission.CALL_PHONE" /> 

答案 1 :(得分:0)

试试这个,

public void getPhoneNumber(String conatctname)
    {
        try
        {
            ContentResolver cr =getContentResolver();
            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            while (cursor.moveToNext()) 
            {

                FirstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                if(FirstName!=null)
                {
                    try
                    {
                    String[] splitval=FirstName.split(" ");
                    if(splitval.length>=1)
                    {
                        FirstName=splitval[0];
                        if(FirstName.equals(conatctname))
                        {
                            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                            {
                            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
                            while (pCur.moveToNext()) 
                            {
                            PhoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            PhoneNumberArray.add(PhoneNumber);
                            }
                            pCur.close();
                        }

                    }
                    }
                    catch(Exception error)
                    {
                        Log.d("SplitError", error.getMessage());
                    }                   

            }
            cursor.close();
        }
        catch (NumberFormatException e)
        {
            e.printStackTrace();
        }
    }