java - update UI from BroadCastReceiver -
how can update main thread broadcastreceiver. have broadcastreceiver receives string , need set text textview on mainactivity, when findviewbyid(r.id.textview);
on onreceive method returns null.
here oncreate mainactivity.
public class mainactivity extends activity { ...... @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); intentfilter broadcastfilter = new intentfilter(responsereceiver.local_action); receiver = new responsereceiver(); localbroadcastmanager localbroadcastmanager = localbroadcastmanager.getinstance(this); localbroadcastmanager.registerreceiver(receiver, broadcastfilter); intent msgintent = new intent(mainactivity.this, intentservice.class); msgintent.putextra(stockdataintentservice.param_in_msg, getresources().getstring(r.string.url)); startservice(msgintent); } public class responsereceiver extends broadcastreceiver { public static final string local_action = "com.mypackage.all_done"; private handler handler; @override public void onreceive(context context, intent intent) { string text =intent.getstringextra(intentservice.param_out_msg); textview txterror = (textview) findviewbyid(r.id.error); } }
op question having exact same issue, accepted answer explains use handler etc.. reading comments on answer explain broadastreceiver runs on main thread there no reason use handler. how come unable set text on text view ?
when findviewbyid(r.id.textview); on onreceive method returns null.
because not calling setcontentview
method in oncreate
method of activity set layout current activity
so, call setcontentview
in oncreate passing layout in error
textview available
Comments
Post a Comment