根据可用性的GPS和网络提供商之间的切换可用性、提供商、根据、网络

2023-09-06 03:04:55 作者:可惜沒如果

 公共无效的onCreate(){
 LocationListener的=新GeoUpdateHandler();
 locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);

尝试 {
   gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
   network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}赶上(例外前){
}

如果(gps_enabled){
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,TIME_DURATION,100,LocationListener的);
}否则,如果(network_enabled){//检查GSM
   locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,TIME_DURATION,距离,LocationListener的);
}
}
公共类GeoUpdateHandler实现LocationListener的{
私人无效的get presentLocation(位置定位){
    ...
            //获取位置
            ...
}

@覆盖
公共无效onProviderDisabled(字符串提供商){}

@覆盖
公共无效onProviderEnabled(字符串提供商){}

@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}

@覆盖
公共无效onLocationChanged(位置定位){
    获得presentLocation(位置);
}
}
 

这是我的code片段,我的问题是,一旦应用程序与GPS安装接通,那么就不会切换到GSM获取位置,一旦我们关闭GPS,反之亦然。

所以,请告诉我一个办法根据位置提供的可用性高效GPS和GSM之间的切换。

注:我的应用程序是一个服务

感谢。

编辑:我有一个BroadcastReceiver,这就是为什么我承担的onCreate()将被一次GPS上调用/ OFF发生

我也试过检查onLocationChanged(位置定位)的供应商的可用性。 但它没有帮助。

编辑:我修改了onProviderDisabled和放大器; onProviderEnabled这样, 请告诉我在做什么错在这里。

 公共无效onProviderDisabled(字符串提供商){
        locationManager.removeUpdates(LocationListener的);
        如果(provider.toLowerCase()。等于(LocationManager.GPS_PROVIDER)){
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,TIME_DURATION,1,LocationListener的);
        } 其他 {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,TIME_DURATION,1,LocationListener的);
        }
    }

    @覆盖
    公共无效onProviderEnabled(字符串提供商){
        locationManager.removeUpdates(LocationListener的);
        如果(provider.toLowerCase()。等于(LocationManager.GPS_PROVIDER)){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,TIME_DURATION,1,LocationListener的);
        } 其他 {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,TIME_DURATION,1,LocationListener的);
        }

    }
 

解决方案

得到了一个解决方案:

 公共无效onProviderDisabled(字符串提供商){
   locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
   如果(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LocationListener的);
   } 其他 {
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationListener的);
   }
}

@覆盖
公共无效onProviderEnabled(字符串提供商){
  locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
  如果(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LocationListener的);
  } 其他 {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationListener的);
  }
}
 
销售网络时间同步产品,网络时间同步产品贸易 网络通讯设备

我错过的是 locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE); 在onProviderEnabled()和onProviderDisabled()。

感谢

public void onCreate() {
 locationListener = new GeoUpdateHandler();
 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

try {
   gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
   network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) { 
}

if(gps_enabled) {
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 100, locationListener);
} else if(network_enabled) { // Checking for GSM
   locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, DISTANCE, locationListener);
}   
}   
public class GeoUpdateHandler implements LocationListener {
private void getPresentLocation(Location location) {
    ... 
            //Fetch the locations
            ...
}

@Override
public void onProviderDisabled(String provider) { }

@Override
public void onProviderEnabled(String provider) { }

@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }

@Override
public void onLocationChanged(Location location) {
    getPresentLocation(location);
}
}

this is my code snippet and my issue is that once the application is installed with GPS Switched ON, then it is not switching to GSM to fetch location once we OFF GPS, and vice versa.

So please tell me a way to switch efficiently between GPS and GSM according to the availability of the location provider.

NOTE: My app is a service.

Thanks.

Edit: I got a BroadcastReceiver, thats why i assume the onCreate() will be invoked once the GPS ON/OFF happen.

I also tried checking the provider availability in onLocationChanged(Location location). But it didnt helped.

EDIT: I modified my onProviderDisabled & onProviderEnabled like this, Please tell what i am doing wrong here..

    public void onProviderDisabled(String provider) {
        locationManager.removeUpdates(locationListener);
        if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);               
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
        }   
    }

    @Override
    public void onProviderEnabled(String provider) {
        locationManager.removeUpdates(locationListener);
        if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
        } else {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);
        }

    }

解决方案

Got a solution for this:

 public void onProviderDisabled(String provider) {
   locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,  locationListener);
   } else {
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
   } 
}

@Override
public void onProviderEnabled(String provider) {
  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
  if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  } else {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  }
}

what i missed is locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); in onProviderEnabled() and onProviderDisabled().

Thanks