android - Send latitude and Longitude from the BoadcastReceiver when the internet connection is avialable -
i want send json string
{ "latitude": 53.86898504, "longitude": 10.66561187, "time": "25.04.2015 11:37:11", "route": 4 }
to server every 60 seconds when try send json string internet connection broadcastreceiver json string null there when send data onlocationchanged method getting string in postdata class want send data when internet connection avialable if internet not avialable store string short time. how can implement jsonstring in connectivity_action broadcastreceiver?
i appreciate help.
mainactivity class:
public class mainactivity extends actionbaractivity { location location; locationmanager locationmanager; string jsonstring; textview textjson; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textjson = (textview) findviewbyid(r.id.textjson); locationmanager lm = (locationmanager) getsystemservice(context.location_service); locationlistener ll = new mylocationlistener(); lm.requestlocationupdates(locationmanager.gps_provider, 10000, 0, ll); } private class broadcastreceiverlistener extends broadcastreceiver { @override public void onreceive(context context, intent intent) { if (intent.getaction().equals( android.net.wifi.wifimanager.scan_results_available_action)) { //code strongest wifi access point in json string routes's value. } else if (intent.getaction().equals( android.net.connectivitymanager.connectivity_action)) { connectivitymanager connectivitymanager = (connectivitymanager) context .getsystemservice(context.connectivity_service); networkinfo netinfo = connectivitymanager .getactivenetworkinfo(); boolean isconnected = netinfo != null && netinfo.isconnectedorconnecting(); if (isconnected) { toast.maketext(context, "the device connected internet ", toast.length_short).show(); if (location == null) { location locat = locationmanager .getlastknownlocation(locationmanager.gps_provider); if (locat == null) { locationlistener loclis = new mylocationlistener(); locationmanager locman = (locationmanager) getsystemservice(context.location_service); criteria criteria = new criteria(); looper mylooper = looper.mylooper(); locman.requestsingleupdate(criteria, loclis, mylooper); } else { system.out.println("locat not null"); } } else { postdata sender = new postdata(); sender.timer(jsonstring); textjson.settext(jsonstring); } } else { toast.maketext(context, "please connect device internet.", toast.length_short).show(); } } } } class mylocationlistener implements locationlistener { @override public void onlocationchanged(location location) { if (location != null) { double plong = location.getlongitude(); double plat = location.getlatitude(); ... string time = sdf.format(location.gettime()); jsonstring = converttojson(plong, plat, time); system.out.println("the output of onlocationchanged: "+ jsonstring); //the code works fine here. json string has values here in broadcastreceiver json string has null. // postdata sender = new postdata(); // sender.timer(jsonstring); // textjson.settext(jsonstring); } } } }
it looks desired behavior adding flag instance variable determine if got location update no internet, , making isconnected
flag instance variable.
the idea send location data every time location changed event if there connection internet, otherwise set flag , wait until there internet connection send data last location update.
i took original code posted in question , modified it:
public class mainactivity extends actionbaractivity { int route_number; double plong; double plat; string time; string jsonstring; boolean isconnected; //added boolean locationupdatednointernet; //added long lastlocationtime = 0; //added textview textjson; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textjson = (textview) findviewbyid(r.id.textjson); locationmanager lm = (locationmanager) getsystemservice(context.location_service); locationlistener ll = new mylocationlistener(); lm.requestlocationupdates(locationmanager.gps_provider, 10000, 0, ll); } private class broadcastreceiverlistener extends broadcastreceiver { @override public void onreceive(context context, intent intent) { if (intent.getaction().equals( android.net.wifi.wifimanager.scan_results_available_action)) { //code strongest wifi access point in json string routes's value. } else if (intent.getaction().equals( android.net.connectivitymanager.connectivity_action)) { connectivitymanager connectivitymanager = (connectivitymanager) context .getsystemservice(context.connectivity_service); networkinfo netinfo = connectivitymanager .getactivenetworkinfo(); //set instance variable isconnected here instead of creating local variable isconnected = netinfo != null && netinfo.isconnectedorconnecting(); //boolean isconnected = netinfo != null //&& netinfo.isconnectedorconnecting(); if (isconnected) { toast.maketext(context, "the device connected internet ", toast.length_short).show(); //check if got location update no internet long currtime = system.currenttimemillis(); if (locationupdatednointernet == true) { //check last location send longer 5 seconds ago if (lastlocationtime == 0 || ((currtime - lastlocationtime) > 5000 )){ lastlocationtime = system.currenttimemillis(); //set last sent time locationupdatednointernet = false; //re-set false postdata sender = new postdata(); //since checking locationupdatednointernet flag, have location data. system.out.println("the output of internet broadcast: " + jsonstring); sender.timer(jsonstring); textjson.settext(jsonstring); } } } else { toast.maketext(context, "please connect device internet.", toast.length_short).show(); } } } } class mylocationlistener implements locationlistener { @override public void onlocationchanged(location location) { if (location != null) { plong = location.getlongitude(); plat = location.getlatitude(); //... time = sdf.format(location.gettime()); jsonstring = converttojson(plong, plat, time); system.out.println("the output of onlocationchanged: "+ jsonstring); long currtime = system.currenttimemillis(); if (isconnected == true) { //check last location send longer 5 seconds ago if (lastlocationtime == 0 || ((currtime - lastlocationtime) > 5000 )) { //if connected, send data lastlocationtime = system.currenttimemillis(); //set last sent time locationupdatednointernet = false; postdata sender = new postdata(); sender.timer(jsonstring); textjson.settext(jsonstring); } } else{ //set flag last location update sent once connection established locationupdatednointernet = true; } } } } }
Comments
Post a Comment