Android Phone book call related

Source: Internet
Author: User

/ ** Get contact name by phone number.*/     Public Staticstring Getcontactnamebyphonenumber (context context, String address) {string[] projection={ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}; //add yourself to the Mspeerscursor cursor =context.getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, Projection,//which columns to return.ContactsContract.CommonDataKinds.Phone.NUMBER + "=" + address + "'",//WHERE clause.                NULL,//WHERE clause value substitution                NULL);//Sort order.        if(Cursor = =NULL) {log.d (TAG,"GetPeople Null"); return NULL; }         for(inti = 0; I < Cursor.getcount (); i++) {cursor.movetoposition (i); //get name of contact person            intNamefieldcolumnindex =cursor. Getcolumnindex (ContactsContract.PhoneLookup.DISPLAY_NAME); String name=cursor.getstring (Namefieldcolumnindex); returnname; }        return NULL; }/*** Get all contact details *@paramContext *@paramAddress *@return     */     Public StaticString getcontacts (context context) {StringBuilder SB=NewStringBuilder (); Contentresolver CR=Context.getcontentresolver (); Cursor Cursor= Cr.query (ContactsContract.Contacts.CONTENT_URI,NULL,                NULL,NULL,NULL); if(Cursor.movetofirst ()) { Do{String contactId=cursor.getstring (cursor. Getcolumnindex (contactscontract.contacts._id)); String name=cursor. getString (cursor Getcolumnindex (contactscontract.conta Cts.                Display_name)); //The first one doesn't have to be wrapped.                if(sb.length () = = 0) {sb.append (name); }Else{sb.append ("\ n" +name); } Cursor Phones=cr.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ "=" + ContactId,NULL,NULL);  while(Phones.movetonext ()) {String PhoneNumber=phones getString (phones. Getcolumnindex (Contactscontra Ct.                    CommonDataKinds.Phone.NUMBER)); //Add Phone InfoSb.append ("\ t"). Append (PhoneNumber);                            } phones.close (); }  while(Cursor.movetonext ());        } cursor.close (); returnSb.tostring ();

-----------------------------Permissions-------------:

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

---------------------Call the system phone book directly:--------------------

Startactivityforresult (new  Intent (intent.action_pick,                    0);
@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        //TODO auto-generated Method StubConfig.putlog ("Requestcode:" + Requestcode + "ResultCode:" +ResultCode); if(ResultCode = =ACTIVITY.RESULT_OK) {            //ContentProvider Show data like a single database table//the Contentresolver instance band method can be implemented to find the specified ContentProvider and obtain the data to ContentProviderContentresolver Recontentresolverol =Getcontentresolver (); //URI, each contentprovider defines a unique public URI that is used to specify the dataset to which it isUri Contactdata =Data.getdata (); //The query is the input URI and other parameters, where the URI is required, and the other is optional, if the system can find the URI corresponding to the ContentProvider will return a cursor object.cursor cursor = managedquery (Contactdata,NULL,NULL,NULL,NULL);            Cursor.movetofirst (); //get the name from the data tablePhone_name =cursor.getstring (cursor. Getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME)); //the condition is the contact ID.String contactId =cursor.getstring (cursor. Getcolumnindex (contactscontract.contacts._id)); //get the phone number from the data table, as the contact ID, because the phone number may have more than oneCursor phone =recontentresolverol.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ "=" + ContactId,NULL,NULL); if(Phone.getcount () > 1) {phone_numbers=NewString[phone.getcount ()]; }            intCount = 0;  while(Phone.movetonext ()) {Phone_number=phone. getString (Phone.getcolumnindex (Contactscontract.commond AtaKinds.Phone.NUMBER)). Replace ("+86", "" "). Replace (" "," "); Config.putlog ("Phone Number---" +phone_number); if(Phone.getcount () > 1) {Phone_numbers[count]=Phone_number; ++count; }            }            if(Phone.getcount () > 1) {Dialog Alertdialog=NewAlertdialog.builder ( This). Settitle ("Please select a phone number"). Setitems (Phone_numbers,NewDialoginterface.onclicklistener () {@Override /c7> Public voidOnClick (Dialoginterface dialog,intwhich) {Phone_number=Phone_numbers[which]; if(Config.ismobileno (Phone_number)) {et_mobile.settext (phone_number);                                        Tv_phone_name.settext (Phone_name); } Else{tv_isp.settextcolor (color.red); Tv_isp.settext ("Wrong number format"); }}). Setnegativebutton (Cancel,                                NewDialoginterface.onclicklistener () {@Override /c3> Public voidOnClick (Dialoginterface dialog,intwhich)                {}}). Create ();            Alertdialog.show (); } Else{et_mobile.settext (phone_number);                Tv_phone_name.settext (Phone_name); if(!Config.ismobileno (Phone_number))                    {Tv_isp.settextcolor (color.red); Tv_isp.settext ("Wrong number format"); }            }        }        Super. Onactivityresult (Requestcode, ResultCode, data); }

Android phone book call related

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.