• <tfoot id='tKGsA'></tfoot>

          <bdo id='tKGsA'></bdo><ul id='tKGsA'></ul>
        <legend id='tKGsA'><style id='tKGsA'><dir id='tKGsA'><q id='tKGsA'></q></dir></style></legend>

        <i id='tKGsA'><tr id='tKGsA'><dt id='tKGsA'><q id='tKGsA'><span id='tKGsA'><b id='tKGsA'><form id='tKGsA'><ins id='tKGsA'></ins><ul id='tKGsA'></ul><sub id='tKGsA'></sub></form><legend id='tKGsA'></legend><bdo id='tKGsA'><pre id='tKGsA'><center id='tKGsA'></center></pre></bdo></b><th id='tKGsA'></th></span></q></dt></tr></i><div id='tKGsA'><tfoot id='tKGsA'></tfoot><dl id='tKGsA'><fieldset id='tKGsA'></fieldset></dl></div>
      1. <small id='tKGsA'></small><noframes id='tKGsA'>

        根据可用性在 gps 和网络提供商之间切换

        时间:2023-09-01
          <tbody id='m53o9'></tbody>

        • <bdo id='m53o9'></bdo><ul id='m53o9'></ul>
        • <legend id='m53o9'><style id='m53o9'><dir id='m53o9'><q id='m53o9'></q></dir></style></legend>

          <small id='m53o9'></small><noframes id='m53o9'>

              <tfoot id='m53o9'></tfoot>

            • <i id='m53o9'><tr id='m53o9'><dt id='m53o9'><q id='m53o9'><span id='m53o9'><b id='m53o9'><form id='m53o9'><ins id='m53o9'></ins><ul id='m53o9'></ul><sub id='m53o9'></sub></form><legend id='m53o9'></legend><bdo id='m53o9'><pre id='m53o9'><center id='m53o9'></center></pre></bdo></b><th id='m53o9'></th></span></q></dt></tr></i><div id='m53o9'><tfoot id='m53o9'></tfoot><dl id='m53o9'><fieldset id='m53o9'></fieldset></dl></div>

                1. 本文介绍了根据可用性在 gps 和网络提供商之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  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);
                  }
                  }
                  

                  这是我的代码片段,我的问题是,一旦在 GPS 开启的情况下安装了应用程序,那么一旦我们关闭 GPS,它就不会切换到 GSM 来获取位置,反之亦然.

                  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.

                  所以请告诉我一种根据位置提供商的可用性在 GPS 和 GSM 之间有效切换的方法.

                  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.

                  谢谢.

                  我有一个广播接收器,这就是为什么我假设一旦 GPS 开/关发生就会调用 onCreate().

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

                  我还尝试在 onLocationChanged(Location location) 中检查提供程序的可用性.但它没有帮助.

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

                  我修改了我的 onProviderDisabled &onProviderEnabled 像这样,请告诉我在这里做错了什么..

                  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);
                          }
                  
                      }
                  

                  推荐答案

                  得到了解决方案:

                   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);
                    }
                  }
                  

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

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

                  谢谢

                  这篇关于根据可用性在 gps 和网络提供商之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:IOS:将图像添加到自定义 MKAnnotationview 下一篇:Android中如何知道定位模式是WIFI还是2G/3G基站?

                  相关文章

                  最新文章

                    <small id='cCen8'></small><noframes id='cCen8'>

                    <tfoot id='cCen8'></tfoot>
                    <legend id='cCen8'><style id='cCen8'><dir id='cCen8'><q id='cCen8'></q></dir></style></legend>

                      • <bdo id='cCen8'></bdo><ul id='cCen8'></ul>
                    1. <i id='cCen8'><tr id='cCen8'><dt id='cCen8'><q id='cCen8'><span id='cCen8'><b id='cCen8'><form id='cCen8'><ins id='cCen8'></ins><ul id='cCen8'></ul><sub id='cCen8'></sub></form><legend id='cCen8'></legend><bdo id='cCen8'><pre id='cCen8'><center id='cCen8'></center></pre></bdo></b><th id='cCen8'></th></span></q></dt></tr></i><div id='cCen8'><tfoot id='cCen8'></tfoot><dl id='cCen8'><fieldset id='cCen8'></fieldset></dl></div>