15.5 Challenge Exercise: Sharecompat The first exercise is relatively straightforward. The Android support Library has a class called Sharecompat, which has a IntentBuilder.ShareCompat.IntentBuilder inner class. Using this inner class to create a Intent for sending a message button is slightly easier. So the challenge for you is to use Sharecompat.intentbuilder to create your Intent in Mreportbutton's listener.
Modify the clicks of the Mreportbutton listener in Crimefragment.
1Mreportbutton.setonclicklistener (NewView.onclicklistener () {2 @Override3 Public voidOnClick (View v) {4Sharecompat.intentbuilder Intentbuilder =ShareCompat.IntentBuilder.from (Getactivity ());5Intentbuilder.settype ("Text/plain");6 Intentbuilder.settext (Getcrimereport ());7 Intentbuilder.setsubject (getString (R.string.crime_report_subject));8 intentbuilder.createchooserintent ();9 Intentbuilder.startchooser ();Ten One //The following is the original code A //Intent i = new Intent (intent.action_send); - //I.settype ("Text/plain"); - //I.putextra (Intent.extra_text,getcrimereport ()); the //I.putextra (Intent.extra_subject, getString (R.string.crime_report_subject)); - // //Create a call to the Createchooser () method for each activity selector that is displayed each time. - //startactivity (Intent.createchooser (i, "select Message App")); - } +});
15.6 Challenge Exercise: Another implicit intent= Uri.parse ("tel:5551234"); phone-related intent operations typically have two types: Intent.action_dial and Intent.action_ Call. Action_call directly calls up the phone app and dials the phone number from intent, while action_dial dials the number and waits for the user to initiate the call. We recommend using the action_dial operation. In this way, the user has the opportunity to calm down and change his mind. This thoughtful design should be welcomed.
Persistent storage for the suspect phone. So first you should add a field for the suspect's phone number in the crime class.
1 Private String Mnumber; 2 3 Public String GetNumber () {4 return Mnumber; 5 }67public void Setnumber (String number) { 8 mnumber = number ; 9 }
Modify Crimebasehelper's OnCreate () method after adding fields
1 @Override2 Public voidonCreate (Sqlitedatabase db) {3Db.execsql ("CREATE TABLE" + Crimetable.name + "(" +4"_id Integer primary Key autoincrement," +5CrimeTable.Cols.UUID + "," +6CrimeTable.Cols.TITLE + "," +7CrimeTable.Cols.DATE + "," +8CrimeTable.Cols.SOLVED + "," +9CrimeTable.Cols.SUSPECT +"," +10 CrimeTable.Cols.NUMBER + //Add a number field to the database One")" A ); -}
Then modify the Getcrime () method in the Crimecursorwrapper class.
1 PublicCrime Getcrime () {2String uuidstring =getString (Getcolumnindex (CrimeTable.Cols.UUID));3String title =getString (Getcolumnindex (CrimeTable.Cols.TITLE));4 LongDate =Getlong (Getcolumnindex (CrimeTable.Cols.DATE));5 intissolved =getInt (Getcolumnindex (CrimeTable.Cols.SOLVED));6String suspect =getString (Getcolumnindex (CrimeTable.Cols.SUSPECT));7 String number = getString (Getcolumnindex (CrimeTable.Cols.NUMBER));8 9Crime Crime =NewCrime (uuid.fromstring (uuidstring));Ten Crime.settitle (title); OneCrime.setdate (NewDate (date)); ACrime.setsolved (issolved! = 0); - Crime.setsuspect (suspect); - crime.setnumber (number); the returncrime; -}
Modify the Getcontentvalues () method in the Crimelab class
1 Private Staticcontentvalues getcontentvalues (Crime Crime) {2Contentvalues values =Newcontentvalues ();3 values.put (CrimeTable.Cols.UUID, Crime.getid (). toString ());4 values.put (CrimeTable.Cols.TITLE, Crime.gettitle ());5 values.put (CrimeTable.Cols.DATE, Crime.getdate (). GetTime ());6Values.put (CrimeTable.Cols.SOLVED, crime.issolved ()? 1:0);7 Values.put (Crimetable.cols.suspect,crime.getsuspect ());8 values.put (CrimeTable.Cols.NUMBER, Crime.getnumber ());9 returnvalues;Ten}
This adds a sentence of Values.put (CrimeTable.Cols.NUMBER, Crime.getnumber ());
Used to update our crime objects later.
Then add a call button to the interface.
Modify the code in Fragment_crime.xml and add a button with the following code:
<button android:id= "@+id/call_suspect" android:layout_marginright= "16DP" android: layout_marginleft= "16DP" android:text= "Call Suspect" android:layout_width= "Match_parent " android:layout_height= "Wrap_content"/>
Then modify the code in the Crimefragment to implement the button's Click event.
Add the following code to the Crimefragment:
Public classCrimefragmentextendsFragment {
........ PrivateButton Mcallsuspectbutton; ........
@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {... mcalls Uspectbutton=(Button) V.findviewbyid (r.id.call_suspect); Mcallsuspectbutton.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//set up Click eventsIntent Intent =NewIntent (intent.action_dial); Uri Number= Uri.parse ("Tel:" +Mcrime.getnumber ()); Intent.setdata (number); StartActivity (Intent); } }); ........ }}
Finally, modify the Onactivityresult () method.
1 @Override2 Public voidOnactivityresult (intRequestcode,intResultCode, Intent data) {3 if(ResultCode! =ACTIVITY.RESULT_OK) {4 return ;5 }6 if(Requestcode = =request_date) {7Date Date =(Date) Data.getserializableextra (datepickerfragment.extra_date);8 mcrime.setdate (date);9 updatedate (Mdatebutton, mcrime);Ten}Else if(Requestcode = = Request_contact && Data! =NULL){ OneUri Contacturi =Data.getdata (); A //a contactscontract.contacts._id is added here to get the target contact ID . -string[] QueryFields =Newstring[]{ - contactscontract.contacts.display_name,contactscontract.contacts._id the }; -Cursor C = getactivity (). Getcontentresolver (). Query (Contacturi,queryfields,NULL,NULL,NULL); - Try{ - if(C.getcount () = = 0){ + return; - } + C.movetofirst (); A //Remove name of contact person atString suspect = c.getstring (0); - //Remove Contact ID. -String Mcontactid = c.getstring (1); - //gets the cursor object by querying the ID. -Cursor phone = getactivity (). Getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL, -ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + Mcontactid,NULL,NULL); in if(Phone.movetonext ()) { - //Remove the contact phone. toString Mphonenumber =phone.getstring (Phone.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)); + //set a phone number for the current crime object - Mcrime.setnumber (mphonenumber); the } * Mcrime.setsuspect (suspect); $ Msuspectbutton.settext (suspect);Panax Notoginseng}finally { - c.close (); the } + } A}
This allows the user to save the suspect's phone number at the same time when the suspect is selected.
Android Authority Programming-Challenge practice Chapter 15.