繁体   English   中英

如果Android手机没有互联网连接,如何将提供商切换到GPS_provider?

[英]How to switch provider into GPS_provider if android phone doesn't have internet connection?

我在android上创建了一个位置应用程序。 这是获取lat和long值的代码。

public void geoLocation()
    {
        locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {

            public void onLocationChanged(Location location) {
              // Called when a new location is found by the network location provider.
                updateWithNewLocation(location);
            }
            public void onStatusChanged(String provider, int status, Bundle extras) {}
            public void onProviderEnabled(String provider) {
                Toast.makeText(ListenSMSservice.this,"Network Enabled",Toast.LENGTH_SHORT ).show(); 
            }
            public void onProviderDisabled(String provider) {
                Toast.makeText(ListenSMSservice.this,"Network Disabled",Toast.LENGTH_SHORT ).show();
            }
          };
        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

    public void updateWithNewLocation(Location location) //update posisi terkini
    {
        if (location != null) {
            lat = location.getLatitude();
            lng = location.getLongitude();

            Geocoder gc = new Geocoder(ListenSMSservice.this, Locale.getDefault());
            try {
              List<Address> addresses = gc.getFromLocation(lat, lng, 1);
              StringBuilder sb = new StringBuilder();
              if (addresses.size() > 0) {
                Address address = addresses.get(0);

                for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                  sb.append(address.getAddressLine(i)).append("\n");

                  sb.append(address.getLocality()).append("\n");
                  sb.append(address.getCountryName());
              }
              addressString = sb.toString();
            } catch (IOException e) {}

            //latLongString = "Lat:" + lat + "\nLong:" + lng;     
            latLongUrl="Phone Map\nhttp://maps.google.com/maps?q=" + lat + ",+" + lng + "+(Your+phone+location)&iwloc=A&hl=en\n";
          } else {
            latLongUrl = "No location found"; 
          }
          myMessage=latLongUrl+"\n"+addressString;
          locationManager.removeUpdates(locationListener); 
          locationManager = null;
          //Toast.makeText(ListenSMSservice.this, myMessage, 3).show();
          sendsms();
    }

如果我有互联网连接( NETWORK_PROVIDER ),该代码将有效。 有没有人知道如何修改该代码, GPS_PROVIDER在Android手机没有任何互联网连接时自动切换到GPS_PROVIDER 谢谢

onLocationUpdate()只检查Internet是否可用,

如果Internet is not available那么,

   locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

这样,

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
    return false;
}

编辑:在这里您可以找到有关Hoe的精美解释,以获取当前位置,

获取用户在Android中的当前位置的最简单,最强大的方法是什么?

请参考这个。

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM