开发者

Error when i need a location android

开发者 https://www.devze.com 2023-03-30 20:41 出处:网络
I have a problem. In my app i need current location from a user. It works fine when we have a location(when i get a location usig method locationManager.getLastKnownLocation), but when i restart my ph

I have a problem. In my app i need current location from a user. It works fine when we have a location(when i get a location usig method locationManager.getLastKnownLocation), but when i restart my phone and location disappear (cash is empty i presume) i get this log and force close app button. I click on a button, my app restarts and then it works fine... I now that a problem is with location that return null... This is the log:

08-25 10:02:55.000: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:55.449: DEBUG/libgps(2006): onUnsol: cmd 0x04 plen 57
08-25 10:02:55.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: num_sv 0
08-25 10:02:55.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: used_in_fix_mask 00000000 
08-25 10:02:55.503: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:55.503: DEBUG/libgps(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:55.503: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:56.006: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:56.006: DEBUG/libg开发者_如何学Gops(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:56.006: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:56.449: DEBUG/libgps(2006): onUnsol: cmd 0x04 plen 57
08-25 10:02:56.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: num_sv 0
08-25 10:02:56.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: used_in_fix_mask 00000000 
08-25 10:02:56.505: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:56.505: DEBUG/libgps(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:56.505: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:57.009: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:57.009: DEBUG/libgps(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:57.009: ERROR/GPS(2006): gen_timer_add_locked


when you restart your phone then all the non-persistent data will be cleared like variable.If you want to save your location then you should use database/sharedPreferences. Also,you can get null location.So you should keep checking to handle.

Edited

You can use Timer class.

Try like this:

 Timer timer1;
  LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  timer1=new Timer();
  timer1.schedule(new GetLastLocation(), 20000);

Following code will be executed after 20 sec.

class GetLastLocation extends TimerTask {
        @Override
        public void run() {
             timer1.cancel();
             lm.removeUpdates(locationListener)
             Location loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
}


Have a look at A Deep Dive Into Location to know pretty much all about how to get a location.


I managed to solve the problem. I created AlertDialog with no buttons and started requestLocationUpdates. In locationListener, when location changed i canceled AlertDialog and that solved my problem :D THANKS jainal and Torsten....

0

精彩评论

暂无评论...
验证码 换一张
取 消