android - Sending Sms in background through AsyncTask is not working -
on click of button want send sms numbers got database in background. if call smsmanager
directly on click works. through asynctask
not working. please me out. here code ..
public class sendsmsactivity extends activity { button buttonsend; edittext textphoneno; spinner level; edittext textsms; private arraylist studentinfo = new arraylist(); private databasehelper mhelper; private sqlitedatabase database; private string levelselected,sms; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.send_sms); buttonsend = (button) findviewbyid(r.id.buttonsend); level = (spinner) findviewbyid(r.id.spinnersms); textsms = (edittext) findviewbyid(r.id.edittextsms); string levelselected = level.getselecteditem().tostring(); string sms = textsms.gettext().tostring(); buttonsend.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string levelselected = level.getselecteditem().tostring(); string sms = textsms.gettext().tostring(); //getphonenum(levelselected, sms); new mysmstask().execute(levelselected, sms); } }); } private void getphonenum(string levelselected, string sms){ mhelper = new databasehelper(this); database = mhelper.getwritabledatabase(); cursor mcursor = database.rawquery("select phone student level= '" + levelselected + "'", null); studentinfo.clear(); if (mcursor.movetolast()) { { string phonenumber = (mcursor.getstring(mcursor.getcolumnindex(databasehelper.phone))); log.d("debug", phonenumber); try { smsmanager smsmanager = smsmanager.getdefault(); smsmanager.sendtextmessage(phonenumber, null, sms, null, null); toast.maketext(getapplicationcontext(), "sms sent!", toast.length_long).show(); } catch (exception e) { toast.maketext(getapplicationcontext(), "sms faild, please try again later!", toast.length_long).show(); e.printstacktrace(); } } while (mcursor.movetoprevious()); } } private class mysmstask extends asynctask<string, string, integer> { @override protected integer doinbackground(string... params) { toast.maketext(getbasecontext(), "senescent " + params, toast.length_short).show(); getphonenum(levelselected, sms); return null; } @override protected void onpostexecute(integer result) { toast.maketext(getbasecontext(), "sent " + " messages", toast.length_short).show(); } @override protected void onpreexecute() {} } }
u can not directly use toast in doinbackground use handler show toast message may that's why problem arise.
please check method getphonenum(levelselected, sms) called or not
private handler handler = new handler(); @override protected integer doinbackground(string... params) { handler.post(new runnable() { @override public void run() { try { toast.maketext(getbasecontext(), "senescent " + params, toast.length_short).show(); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); getphonenum(levelselected, sms); return null; }
Comments
Post a Comment