Android Programming Tutorials

(Romina) #1
Getting the Word Out

else if (item.getItemId()==R.id.sms) {
sendSMS();

return(true);
}

return(super.onOptionsItemSelected(item));
}

Step #2: Find Contacts' Mobile Numbers.........................................


To send an SMS, we could just have Android handle the whole thing, by


passing control to the user's choice of SMS client application. There, they


could pick the contact to send the message to and compose the message,


perhaps starting with some prose supplied by LunchList.


In this case, though, we are going to send the SMS directly. To do that, we


need a phone number of the person to which we should send the message.


The ContactsContract content provider in Android 2.x is the way to find out


these phone numbers. Specifically, we can query it to find all names and


phone numbers, filtering based upon phone type to only get those flagged


as mobile numbers.


With that in mind, add this preliminary implementation of the sendSMS()


method to DetailForm:


private void sendSMS() {
String[] PROJECTION=new String[] { Contacts._ID,
Contacts.DISPLAY_NAME,
Phone.NUMBER
};
String[] ARGS={String.valueOf(Phone.TYPE_MOBILE)};
final Cursor c=managedQuery(Phone.CONTENT_URI,
PROJECTION, Phone.TYPE+"=?",
ARGS, Contacts.DISPLAY_NAME);
}

This gives us a Cursor, containing the ID, name, and mobile phone number


of everyone in our contacts database, sorted by name. The reason why the


Cursor is declared as final will become apparent later in the tutorial.


365
Free download pdf