android - How to get data from service to activity -
in app have activity , service... service broadcast messages gathered data gps... activity should receive broadcast messages , update ui...
my code
public class locationpollerdemo extends activity { private static final int period = 10000; // 30 minutes private pendingintent pi = null; private alarmmanager mgr = null; private double lati; private double longi; private servicereceiver servicereceiver; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mgr = (alarmmanager) getsystemservice(alarm_service); intent = new intent(this, locationpoller.class); i.putextra(locationpoller.extra_intent, new intent(this, servicereceiver.class)); i.putextra(locationpoller.extra_provider, locationmanager.gps_provider); pi = pendingintent.getbroadcast(this, 0, i, 0); mgr.setrepeating(alarmmanager.elapsed_realtime_wakeup, systemclock.elapsedrealtime(), period, pi); debuglog.logtrace("on create demo"); toast.maketext(this, "location polling every 30 minutes begun", toast.length_long).show(); servicereceiver = new servicereceiver(); intentfilter filter = new intentfilter("me"); this.registerreceiver(servicereceiver, filter); } class servicereceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { file log = new file(environment.getexternalstoragedirectory(), "location2.txt"); debuglog.logtrace(environment.getexternalstoragedirectory().getabsolutepath()); try { bufferedwriter out = new bufferedwriter(new filewriter(log.getabsolutepath(), log.exists())); out.write(new date().tostring()); out.write(" : "); bundle b = intent.getextras(); location loc = (location) b.get(locationpoller.extra_location); string msg; if (loc == null) { loc = (location) b.get(locationpoller.extra_lastknown); if (loc == null) { msg = intent.getstringextra(locationpoller.extra_error); } else { msg = "timeout, lastknown=" + loc.tostring(); } } else { msg = loc.tostring(); } if (msg == null) { msg = "invalid broadcast received!"; } out.write(msg); out.write("\n"); out.close(); } catch (ioexception e) { log.e(getclass().getname(), "exception appending log file", e); debuglog.logexception(e); } } } }
when use code not working properly... using servicereceiver class in separate file works fine.... please tell me...!!
in service class wrote this
private static void sendmessagetoactivity(location l, string msg) { intent intent = new intent("gpslocationupdates"); // can include data. intent.putextra("status", msg); bundle b = new bundle(); b.putparcelable("location", l); intent.putextra("location", b); localbroadcastmanager.getinstance(context).sendbroadcast(intent); }
and @ activity side have receive broadcast message
localbroadcastmanager.getinstance(getactivity()).registerreceiver( mmessagereceiver, new intentfilter("gpslocationupdates"));
by way can send message activity. here mmessagereceiver class in class perform ever want....
in code did this....
private broadcastreceiver mmessagereceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { // data included in intent string message = intent.getstringextra("status"); bundle b = intent.getbundleextra("location"); lastknownloc = (location) b.getparcelable("location"); if (lastknownloc != null) { tvlatitude.settext(string.valueof(lastknownloc.getlatitude())); tvlongitude .settext(string.valueof(lastknownloc.getlongitude())); tvaccuracy.settext(string.valueof(lastknownloc.getaccuracy())); tvtimestamp.settext((new date(lastknownloc.gettime()) .tostring())); tvprovider.settext(lastknownloc.getprovider()); } tvstatus.settext(message); // toast.maketext(context, message, toast.length_short).show(); } };
Comments
Post a Comment