It is cold, I want to put the food up, this is not recently in the software park on the second floor to eat, 15 pieces of food, leverage.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/E0/wKiom1Rbd2eggnzrAAMtkr5vUJY784.jpg "style=" float: none; "title=" Qq20141106212649.png "alt=" Wkiom1rbd2eggnzraamtkr5vujy784.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/DF/wKioL1Rbd8yz1plfAAFubshtix0664.jpg "style=" float: none; "title=" Qq20141106212705.png "alt=" Wkiol1rbd8yz1plfaafubshtix0664.jpg "/>
Beauty bag, don't say, get to the point. Today I want to talk about reading contacts, in fact, I do this is intended to do a message interception, phone interception function.
We look at the interface first, or good, very beautiful.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4D/DF/wKioL1RbeTHAM8hyAAIo4XZJDwQ669.jpg "title=" Qq20141106213359.png "alt=" Wkiol1rbetham8hyaaio4xzjdwq669.jpg "/>
OK, we know at a glance that this is another listview. The current feature is that when you tick in the checkboxes, clicking on the back of the dial will call the phone out. If you do not hook, the phone will not be dialed out. OK, let's look at the page layout first
<?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 "><listview android:id=" @+id/contactlistview " android: Descendantfocusability= "blocksdescendants" android:layout_width= "fill_parent" android:layout_height= "fill_parent "Android:divider=" @color/teal "android:dividerheight=" 1DP "></listview><linearlayout android:o rientation= "Horizontal" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" ><Button android:id= "@+id/btnselall" android:text= "@string/btnselall" android:textcolor= "@color/teal" Android: Textsize= "14DP" android:textstyle= "bold" android:layout_weight= "1" android:layout_width= "Fill_parent" Android: layout_height= "Fill_parent" ></button><button android:id= "@+id/btninversesel" android:text= "@ String/btnselinverse "Android:textcolor=" @color/teal "android:layout_marginleft=" 1DP "android:layout_weight=" 1 "android:textsize=" 14DP "android:textstyle=" bold " Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" ></Button><Button Android:id= "@+id/btnset" android:text= "@string/btnset" android:layout_weight= "1" android:textcolor= "@color/teal" android:layout_marginleft= "1DP" android:textsize= "14DP" android:textstyle= "bold" android:layout_width= "Fill_ Parent "android:layout_height=" Fill_parent "></Button></LinearLayout></LinearLayout>
Let's look at the template that the ListView will load
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android "android:layout_width=" fill_parent " android:layout_height=" Wrap_ Content "android:id=" @+id/contacttemplate "><tablelayout android:id=" @+id/tabcontatmain "Android: Layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:stretchcolumns= "2" android: shrinkcolumns= "2" android:padding= "3dip" ><tablerow><checkbox android:id= "@+id/ Chkcontactuser " android:layout_gravity=" center_vertical "></checkbox><imageview android: Id= "@+id/imgcontactphoto" android:layout_gravity= "center_vertical" android:scaletype= "FitCenter" ></ Imageview><textview android:id= "@+id/txtcontactname" android:layout_marginleft= "10DP" android: Layout_gravity= "center_vertical" android:textcolor= "@color/teal1" ></textview><textview android : id= "@+id/txtcontacttelnumber" Android:layout_marginleft= "4DP" android:gravity= "right" android:layout_gravity= "center_vertical" android:textcolor= "@ Color/yellow "></textview><button android:id=" @+id/btndail " android:text=" @string/btndail "Android:textsize=" 10DP "android:layout_marginleft=" 10DP " android:width=" 60DP "android:drawableright=" @ Drawable/dail "android:layout_gravity=" center_vertical " android:textcolor=" @color/purplered "></ Button></tablerow></tablelayout></linearlayout>
is still the tablelayout layout, we set its contraction as the second column, and the stretch column is also the second column. This will shrink if the second column is not displayed. OK, let's look at the backstage code.
public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.punchinalarm); owner = This;btnselall = (Button) This.findviewbyid (r.id.btnselall); btnselinverse = (Button) This.findviewbyid (R.id.btninversesel); btnset = (Button) This.findviewbyid (r.id.btnset); Contactuserlistview = ( ListView) This.findviewbyid (r.id.contactlistview);d atalist = new arraylist<map<string, object>> (); InitData ();}
In the OnCreate method, we initialize the data.
Private void initdata () {String phoneUserName = null; string phonenumber = null; long contactid = null; long photoid = null; map<string, object> datamap = null; Contentresolver resolver = this.getapplicationcontext (). Getcontentresolver ();/* * Get SIM card Contact uri uri = uri.parse ("Content://icc/adn"); */cursor phonecursor = resolver.query (phone.content_uri,phone_projection, null, null, null);if (phoneCursor != null) {while (Phonecursor.movetonext ()) {dataMap = new HashMap< String, object> ();p honeusername = phonecursor.getstring (0);p honenumber = Phonecursor.getstring (1);p Hotoid = phonecursor.getlong (2); Contactid = phonecursor.getlong ( 3);if (phonenumber == null | | phonenumber.trim (). Length () < 11) {continue;} bitmap contactphoto = null;if (photoid > 0) {Uri uri = Contenturis.withappendedid (Contactscontract.contacts.content_uri, contactid);InputStream input = contactscontract.contacts.opencontactphotoinputstream (Resolver, uri);contactPhoto = Bitmapfactory.decodestream (input);} else {contactphoto = bitmapfactory.decoderesource (Getresources (), R.drawable.usersmall);} Datamap.put ("UserName", phoneusername);d atamap.put ("Userphonenumber", phonenumber);d atamap.put (" Userphoto ", contactphoto);d Atalist.add (DATAMAP);} if (Datalist != null && datalist.size () > 0) {customAdapter simpleadapter = new customadapter (This, datalist,r.layout.contactdetailtemplate, new string[] {"Userphoto", "UserName", "Userphonenumber" },new int[] { r.id.chkcontactuser, r.id.imgcontactphoto,r.id.txtcontactname, r.id.txtcontacttelnumber,r.id.btndail }); This.contactUserListView.setAdapter (Simpleadapter);}}}
We know that the external program can access the data provided by the ContentProvider through the Contentresolver interface, and in the activity through Getcontentresolver () can get the current application Contentresolver instance. Because both the address book and the short message can be accessed through the interface, We can access the SIM card and contact information on the phone via getcontentresolver .
We're mainly to the following sentence.
Cursor phonecursor = resolver.query (phone.content_uri,phone_projection, NULL, NULL, NULL);
Through the interface query, we will get a cursor. By looking at the cursor definition, we find that it is an abstract class
Public abstract Interface Android.database.Cursor
We found that it provides some methods, as follows
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4D/DF/wKioL1RbgASRasFnAAEq2WtayDw502.jpg "title=" Qq20141106220202.png "alt=" Wkiol1rbgasrasfnaaeq2wtaydw502.jpg "/>
This shows that it is a non-forward cursor that can both be advanced and backward, similar to a cursor in SQL Server. In this case, we can locate the cursor at any time, whether we are reading the contact information or reading the short message.
In the above we see a few parameters of query, Phone.content_uri, to get the contact person need to go to this URI to fetch data. In fact, the value of the Phone.content_uri here is content://com.android.contacts/contacts. It is defined as follows
public static final Android.net.Uri Content_uri;
OK, after we get the contact, we loop, the cursor moves down, and we get all the contact data with the phone number. Because the order of our incoming phone_projection is name, phone number, photo ID, and a contactid. So we see the order in which the data is taken:
Phoneusername = phonecursor.getstring (0);p honenumber = phonecursor.getstring (1);p hotoid = Phonecursor.getlong (2); ContactId = Phonecursor.getlong (3);
After we get photoid, we judge whether it is greater than 0, and if it is greater than 0, we will get the user image.
To get the user image, first connect the URI and the parameter by the following code.
Contenturis.withappendedid (ContactsContract.Contacts.CONTENT_URI, ContactId)
In fact, this is similar to the Get method of the API, such as contactuser/100, which means to get a number 100 of the information of the person.
After the Ok,uri is constructed, we get the image.
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream (resolver, uri); Contactphoto = Bitmapfactory.decodestream (input);
Finally add List<map<string,object>>, apply adapter to the ListView.
Class customadapter extends baseadapter {private list<map<string, object >> datalist;private layoutinflater minflater;private context context;private String[] keyString;private int[] valueViewID; Holder holder;public customadapter (context context,list<map<string, object>> datalist, int resource,string[] from, int[] to) {this.dataList = dataList;this.context = context;mInflater = (Layoutinflater) This.context.getSystemService (context.layout_inflater_service); keystring = new string[ from.length];valueviewid = new int[to.length]; System.arraycopy (from, 0, keystring, 0, from.length); System.arraycopy (to, 0, valueviewid, 0, to.length);} @Overridepublic int getcount () {return datalist.size ();} @Overridepublic object&nbsP;getitem (int position) {return datalist.get (position);} @Overridepublic long getitemid (int position) {return position;} Public void removeitem (int position) {datalist.remove (position); This.notifydatasetchanged ( );} Public view getview (int position, view convertview, viewgroup parent) {if (convertview != null) {holder = (holder) convertview.gettag ();} else {convertview = minflater.inflate (r.layout.contactdetailtemplate,null); holder = new holder ();holder.chkcontactuser = (CheckBox) convertview.findviewbyid (ValueViewID [0]); holder.imguserphoto = (ImageView) convertview.findviewbyid (valueviewid[1]); holder.labUserName = (TextView) convertview.findviewbyid (valueviewid[2]);holder.labphonenumber = ( TextView) convertview.findviewbyid (valueviewid[3]); HOLDER.BTNDAIL&NBSP;=&NBSP; (Button) convertview.findviewbyid (valueviewid[4]); Convertview.settag (holder);} Map<string, object> appinfo = datalist.get (position);if (appInfo != NULL) {string username = appinfo.get (keystring[1]). toString (); String userphonenumber = appinfo.get (keystring[2]) == null ? "": Appinfo.get (Keystring[2]). toString (); bitmap userphoto = (Bitmap) appinfo.get (keystring[0]); Holder.labUserName.setText (UserName) ; Holder.labPhoneNumber.setText (Userphonenumber); Holder.imgUserPhoto.setImageBitmap (Userphoto); Holder.btnDail.setOnClickListener (New viewbuttonlistener (Position, holder.chkcontactuser));} Return convertview;}} Class holder {public textview labusername;public textview labphonenumber;public imageview imguserphoto;public button btndail;public checkbox chkcontactuser;}
Here, it is very simple, we just get the control, and then according to position, get a list of rows of data, and then assign values.
Finally we look at the button's Click event and we pass in the position and the checkbox for each line.
Class Viewbuttonlistener implements Onclicklistener {private int position;object phonenumber; CheckBox Chkcontactuser; Viewbuttonlistener (int position,checkbox chkcontactuser) {this.position = Position;this.phonenumber = DataList.get ( Position). Get ("Userphonenumber"); this.chkcontactuser= Chkcontactuser;} @Overridepublic void OnClick (view view) {int vid = View.getid (); if (vid = = r.id.btndail&& Chkcontactuser.ischecked ()) {Intent dialintent = new Intent (Intent.action_call, Uri.parse ("Tel:" + phonenumber)); StartActivity (dialintent);}}}
In the onclick, we determine if the Dial button and the checkbox of the column header are checked, it will be dialed, otherwise it will not be dialed. OK, in the end we want to press the fallback button when the popup whether to exit the prompt
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4D/E0/wKiom1RbjdHDxnOlAAGKTIuOxw4762.jpg "title=" Qq20141106230340.png "alt=" Wkiom1rbjdhdxnolaagktiuoxw4762.jpg "/>
OK, the code is as follows
public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back && Event.getrepeatco UNT () = = 0) {dialog (); return true;} return true;} protected void Dialog () {Alertdialog.builder builder = new Builder (punchinalarm.this); Builder.setmessage ("OK to exit?"); Builder.settitle ("hint"); Builder.setpositivebutton ("Confirm", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Dialog.dismiss (); Android.os.Process.killProcess ( Android.os.Process.myPid ());}); Builder.setnegativebutton ("Cancel", new Android.content.DialogInterface.OnClickListener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Dialog.dismiss ();}}); Builder.create (). Show ();
Finally, the man's blog is genuine, Xiaomi 3 test machine.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4D/E0/wKiom1Rbj22DJ0SbAAHqhJl9btY782.jpg "title=" Qq20141106231030.png "alt=" wkiom1rbj22dj0sbaahqhjl9bty782.jpg "/>650" this.width=650; "src=" http://s3.51cto.com /wyfs02/m00/4d/e0/wkiom1rbj6drduvhaatc_ri71si887.jpg "title=" Qq20141106231122.png "alt=" wKiom1Rbj6DRdUVHAATC_ Ri71si887.jpg "/>
This article is from the "Technology Creation value" blog, please be sure to keep this source http://leelei.blog.51cto.com/856755/1573863
Android closest combat (eight)