Android Programming Tutorials

(Romina) #1
Getting the Word Out

Note that you will need to add the READ_CONTACTS permission to get this to


work, since we are directly accessing the contacts' data. Also, you will need


to add imports for android.provider.ContactsContract.Contacts and


android.provider.ContactsContract.CommonDataKinds.Phone.


Step #3: Pick a Person........................................................................


Now, we need to let the user pick to whom we should send the SMS. Given


the Cursor, we can display a ListView, or a Spinner, or something to let the


user make a selection. However, those typically imply another Activity, and


that seems a bit much for just picking a person by name. Instead, we will


use an AlertDialog.


So, add the following to the end of sendSMS():


new AlertDialog.Builder(this)
.setTitle("Pick a Person")
.setCursor(c, onSMSClicked, Contacts.DISPLAY_NAME)
.show();

Here, we pop up an AlertDialog, supplying the Cursor, indicating that the


entries in the AlertDialog's list should be made from the DISPLAY_NAME


column. We will find out the user's selection, if any, in an onSMSClicked


listener object that will be defined in the next section.


You will need to add an import to android.app.AlertDialog for this to


compile.


Step #4: Send the Message


Now, we can add the code to find out the user's choice of recipient and send


the SMS message.


First, add the onSMSClicked object definition to sendSMS(), giving us:


private void sendSMS() {
String[] PROJECTION=new String[] { Contacts._ID,

366
Free download pdf