i have service request network location updates in it's oncreatemethod. have used locationlistener receive new locations, status of providers , statuschange in provider.
while device in low power state(phone screen off), same location fix network provider when phone screen active(screen on).
public class locationregisterservice extends service implements locationlistener{ private string tag = locationregisterservice.class.getname(); private locationmanager lm; public void oncreate() { log.d(tag,"entering locationregisterservice::oncreate()"); //register network location updates lm = (locationmanager) this.getsystemservice(context.location_service); log.i(tag,"regsitering network location updates"); log.i(tag,"mintime = 0,mindistance = 0"); lm.requestlocationupdates(locationmanager.network_provider, 0, 0, this); log.d(tag,"exiting locationregisterserivce::oncreate()"); } public int onstartcommand(intent intent, int flags, int startid) { log.d(tag,"entering locationregisterservice::onstartcommand()"); log.d(tag,"exiting locationregisterservice::onstartcommand()"); return service.start_sticky; } @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } public void ondestroy() { super.ondestroy(); log.d(tag,"entering locationregisterservice::ondestroy()"); lm.removeupdates(this); log.d(tag,"exiting locationregisterservice::ondestroy()"); } @override public void onlocationchanged(location location) { helpers.logscreenstatus(this); double latitude = location.getlatitude(); double longitude = location.getlongitude(); double accuracy = location.getaccuracy(); string location_timestamp = formattime(location.gettime()); string log = latitude + "," + longitude +","+accuracy+","+location_timestamp; log.i(tag,log); } @override public void onproviderdisabled(string provider) { string log = "network provider disabled"; } @override public void onproviderenabled(string provider) { string log = "network provider enabled"; } @override public void onstatuschanged(string provider, int status, bundle extras) { string log = ""; switch(status){ case locationprovider.available: log = "network available"; break; case locationprovider.out_of_service: log = "network out of service"; break; case locationprovider.temporarily_unavailable: log = "network temporarily unavailable"; break; } setservicewakeuptime(); stopself(); } private void setservicewakeuptime() { alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); intent intent = new intent(this,alarmreceiver.class); pendingintent operation = pendingintent.getbroadcast(this,0,intent,pendingintent.flag_update_current); long wakeat = system.currenttimemillis() + (60 * 1000); am.set(alarmmanager.rtc_wakeup, wakeat , operation); filelogger.log(this, "service wake after 1 minute"); } private string formattime(long epoch) { date date = new date(epoch); string pattern = "y/m/d h:m:s.s"; simpledateformat dateformat = new simpledateformat(pattern); return dateformat.format(date); } }
i started service activity , travelled 50km device screen off. attaching logs of travel.
2013/3/21 0:29:58.743 entering broadcast receiver 2013/3/21 0:29:58.756 screen off 2013/3/21 0:29:58.778 exiting broadcast receiver 2013/3/21 0:29:58.783 registering network location updates 2013/3/21 0:30:53.571 screen off 2013/3/21 0:30:53.587 13.07383585,80.22151335000001,50.0,2013/3/21 0:30:8.551 2013/3/21 0:30:53.593 screen off 2013/3/21 0:30:53.595 network temporarily unavailable 2013/3/21 0:30:53.613 service wake after 1 minute 2013/3/21 0:30:53.619 unregistering listener location updates 2013/3/21 0:31:53.676 entering broadcast receiver 2013/3/21 0:31:53.689 screen off 2013/3/21 0:31:53.710 exiting broadcast receiver 2013/3/21 0:31:58.516 registering network location updates 2013/3/21 0:32:17.583 screen off 2013/3/21 0:32:17.594 13.07383585,80.22151335000001,50.0,2013/3/21 0:32:3.544
the same log repeated hour. code run in htc explorer android 2.3.3. have answer behavior ?
the problem of getting same location updates happening in htc mobile phones. don't know whether occurs on htc android phones. occurs sure on htc explorer.
the reason getting same location updates in background is, once phone inactive more 15 minutes, htc disable add active internet connections (wifi or mobile data connection). cross check this, wrote following code.
- an activity schedule alarmreceiver called after 1 minute.
- the alarm receiver checks whether there connection through internet connection can established.
- the alarm receiver registers alarm call after 1 minute
i made code run through out night.
public class alarmreceiver extends broadcastreceiver { private static string tag = alarmreceiver.class.getname(); @override public void onreceive(context context, intent intent) { log.i(tag, "entering broadcast receiver onreceive"); filelogger.log(context, "entering broadcast receiver"); logactivenetworkinfo(context); setalarm(context); filelogger.log(context, "exiting broadcast receiver"); log.i(tag, "exiting broadcast receiver onreceive"); } private void logactivenetworkinfo(context context) { connectivitymanager connectivitymanager = (connectivitymanager) context.getsystemservice(context.connectivity_service); networkinfo networkinfo = connectivitymanager.getactivenetworkinfo(); if(networkinfo == null) { filelogger.log(context, "no network connectivity"); return; } string networktypename = networkinfo.gettypename(); boolean isconnected = networkinfo.isconnected(); string log = ""; if(isconnected == true) { log = "internet available through " + networktypename; } else log = "internet not available through "+ networktypename; filelogger.log(context, log); log.i(tag,log); return; } private void setalarm(context context) { alarmmanager alarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service); long wakeupat = system.currenttimemillis() + (60 * 1000); intent intent = new intent(context, alarmreceiver.class); pendingintent operation = pendingintent.getbroadcast(context, 0, intent, pendingintent.flag_update_current); alarmmanager.set(alarmmanager.rtc_wakeup, wakeupat , operation); log.i(tag,"alarm set ring after "+wakeupat + " milliseconds"); }
}
so app developers careful, if want make http call in background in htc phone, fail if phone inactive more fifteen minutes.
this issue has been reported htc deep sleep
Comments
Post a Comment