android - How to detect GPS being enabled -
android - How to detect GPS being enabled -
i'm making app uses gps. problem comes when open app without having gps enabled, nil (obviously) if enable gps without exiting app app still doing nothing. need disable , enable gps in app need know problem comes from, i'll leave here code:
public class escuchalocalizacion implements locationlistener { private locationmanager gestor; private textview view1; private textview view2; private boolean registrado; private static final float mindistance = 10.0f; private static final long mintime = 300; public double _long; public double _lat; escuchalocalizacion (context c, textview t1, textview t2) { log.v("escuchalocalizacion", "construyendo el objeto"); gestor = (locationmanager) c.getsystemservice(context.location_service); registrado = false; view1 = t1; view2 = t2; log.v("escuchalocalizacion", "he terminado de construir el objeto"); } @override public void onlocationchanged(location arg0) { // todo auto-generated method stub _long = arg0.getlongitude(); _lat = arg0.getlatitude(); view1.settext(double.tostring(_long)); view2.settext(double.tostring(_lat)); } @override public void onproviderdisabled(string arg0) { // todo auto-generated method stub if(arg0.contentequals(locationmanager.gps_provider)){ log.w("escucharlocalizacion","provider: " + locationmanager.gps_provider + " disabled"); } } @override public void onproviderenabled(string arg0) { // todo auto-generated method stub if(!registrado && arg0.contentequals(locationmanager.gps_provider)){ registrar(); } } @override public void onstatuschanged(string arg0, int arg1, bundle arg2) { // todo auto-generated method stub } public void registrar() { if (gestor.isproviderenabled(locationmanager.gps_provider)){ log.i("escucharlocalizacion", "registrado " + locationmanager.gps_provider); gestor.requestlocationupdates(locationmanager.gps_provider, mintime, mindistance, this); registrado = true; }else { log.w("escucharlocalizacion", "no se pudo registrar " + locationmanager.gps_provider); registrado = false; } } public void anularregistro(){ gestor.removeupdates(escuchalocalizacion.this); registrado = false; } }
where initialize escuchalocalizacion?
you need search location in onresume () , stop searching onpause location in activity.
start locating: escuchalocalizacion.requestlocationupdates(locationmanager.gps_provider, 00, 0, gpslocationlistener);
stop locating: escuchalocalizacion.removeupdates(gpslocationlistener);
if have checkbox enable/disable gps localisation, can add together listener on checkbox set functions there if want utilize gps or not.
also don't forget add together in manifest:
<uses-permission android:name="android.permission.access_coarse_location"></uses-permission> <uses-permission android:name="android.permission.access_fine_location"></uses-permission>
android gps location
Comments
Post a Comment