android - How to bring contacts up to send a text -
i have application defines word , sends definition via sms. problem user has type in phone number. thinking perhaps app should bring contacts list, user can select contact user can send message contact. here code smsactivity:
public class smsactivity extends activity { string apptag = "smstransmit"; //private static strings: private final static string sent = "sms_sent"; private final static string delivered = "sms_delivered"; private button btnsend; private button btnexit; private button btnclear; private edittext etphonenumber; private edittext etusermessage; //private broadcastreceiver member variables: private smsdispatchreceiver sendreceiver = null; private smsreceiptreceiver receiptreceiver = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_sms); bundle extras = getintent().getextras(); string definiedword = null; if (extras != null) { definiedword = extras.getstring("definition"); } else { definiedword = "none"; } log.d("recievedagain", definiedword); log.v(apptag, "mainactivity: oncreate() called"); //create new broadcast receiver sending sms sendreceiver = new smsdispatchreceiver(); //create new broadcast receiver receipt of send sms receiptreceiver = new smsreceiptreceiver(); //register new receivers (as new intentfiler() objects): registerreceiver(sendreceiver, new intentfilter(sent)); registerreceiver(receiptreceiver, new intentfilter(delivered)); //get view objects: btnsend = (button) findviewbyid(r.id.btnsend); btnclear = (button) findviewbyid(r.id.btnclear); btnexit = (button) findviewbyid(r.id.btnexit); etphonenumber = (edittext) findviewbyid(r.id.etnumber); etusermessage = (edittext) findviewbyid(r.id.etmessage); //disable soft keyboard (this in general): inputmethodmanager inmethodmgr = (inputmethodmanager)getsystemservice(context.input_method_service); inmethodmgr.hidesoftinputfrominputmethod(etphonenumber.getwindowtoken(), 0); inmethodmgr.hidesoftinputfrominputmethod(etusermessage.getwindowtoken(), 0); //set hint edittext boxes: etphonenumber.sethint("enter phone number (default = 5556)"); etusermessage.sethint(definiedword); //create click event listener send button (onclicklistener): btnsend.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string strmessage = null; string strnumber = null; //get phone number edittext box: strnumber = etphonenumber.gettext().tostring(); //check phone number: if (strnumber.length() <= 0) { //no phone number, default (5556): strnumber = smsproperties.getphonenumber(); } //get message edittext box: strmessage = etusermessage.gettext().tostring(); //check message contains content: if (strmessage.length() > 0) { //sent sms phone number sendsms(strnumber, strmessage); } else { //warn user , reset focus / hint: toast.maketext(getbasecontext(), "no message text! please enter text sms!!", toast.length_short).show(); etusermessage.sethint("enter message text here . . . "); etusermessage.requestfocus(); } } }); //create click event listener clear button (onclicklistener): btnclear.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //clear appropriate edittext box: if (etusermessage.isfocused()) { //clear text in user message edittext box: etusermessage.settext(""); //set hint in user message edittext box: etusermessage.sethint("enter message text here . . . "); } else if (etphonenumber.isfocused()) { //clear text in phone number edittext box: etphonenumber.settext(""); //set hint in phone number edittext box: etphonenumber.sethint("enter phone number (default = 5556)"); } } }); //create click event listener exit button (onclicklistener): btnexit.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //finish activity: //note: main activity cause ondestroy() called! finish(); } }); } //method send sms message device: private void sendsms(string strnum, string strmsg) { //todo: create pending intent smsdistatchreceiver (sent): pendingintent sentpi = pendingintent.getbroadcast(this, 0, new intent("sent"), 0); //todo: create pending intent smsreceiptreceiver (delivered): pendingintent deliveredpi = pendingintent.getbroadcast(this, 0, new intent("delivered"), 0); //get default smsmanager object: smsmanager smsmgr = smsmanager.getdefault(); //todo: send sms using smsmanager: smsmgr.sendtextmessage(strnum, null, strmsg, sentpi, deliveredpi); }
consider contact button bring contact list
contact.setonclicklistener(new view.onclicklistener() { @override public void onclick(view g) { intent q = new intent(intent.action_pick, contacts.content_uri); startactivityforresult(q, 1001); } });
and handle result (which referenced 1001) use this:
public void onactivityresult(int reqcode, int resultcode, intent data) { super.onactivityresult(reqcode, resultcode, data); if (resultcode == activity.result_ok) { // getting uri result further working uri contactdata = data.getdata(); cursor c = managedquery(contactdata, null, null, null, null); if (c.movetofirst()) { string id =c.getstring(c.getcolumnindexorthrow(contactscontract.contacts._id)); string hasphone =c.getstring(c.getcolumnindex(contactscontract.contacts.has_phone_number)); if (hasphone.equalsignorecase("1")) { cursor phones = getcontentresolver().query( contactscontract.commondatakinds.phone.content_uri,null, contactscontract.commondatakinds.phone.contact_id +" = "+ id, null, null); phones.movetofirst(); //this string hold contact number string cnumber = phones.getstring(phones.getcolumnindex("data1")); //this string hold contact name string cname = c.getstring(c.getcolumnindexorthrow(contactscontract.contacts.display_name)); } }} }
Comments
Post a Comment