Android Programming Tutorials

(Romina) #1
Getting the Word Out

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);
DialogInterface.OnClickListener onSMSClicked=
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int position) {
c.moveToPosition(position);

noReallySendSMS(c.getString( 2 ));
}
};

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

The onSMSClicked listener will find out the phone number for the selected


person (given the clicked-upon cursor position), then call noReallySendSMS()


to create and deliver the SMS message.


Then, add noReallySendSMS() to DetailForm as follows:


private void noReallySendSMS(String phone) {
StringBuilder buf=new StringBuilder("We are going to ");

buf.append(name.getText());
buf.append(" at ");
buf.append(address.getText());
buf.append(" for lunch!");

SmsManager
.getDefault()
.sendTextMessage(phone, null, buf.toString(), null, null);
}

Here, we build up a message, incorporating the name and address from the


EditText widgets. Then, we use SmsManager to send the message to its


desired target.


You will need to add the SEND_SMS permission to your manifest and add


imports for:


367
Free download pdf