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

发布于 2024-12-10 00:27:36 字数 2668 浏览 1 评论 0原文

我在android上制作了一个位置应用程序。这是获取纬度和经度值的代码。

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),该代码将起作用。有谁知道如何修改该代码以在 Android 手机没有任何互联网连接时自动切换到 GPS_PROVIDER 吗?谢谢

i make a location application on android. here's the code for acquiring lat and long value.

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

that code will works if i have an internet connection (NETWORK_PROVIDER). has anyone know how to modify that code for automatically switch into GPS_PROVIDER when android phone doesn't have any internet connection?? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身骄傲 2024-12-17 00:27:36

onLocationUpdate() 只是检查互联网是否可用,

如果 互联网不可用 那么就像

   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 中获取用户当前位置的最简单、最可靠的方法是什么?

另请参阅此内容。

谢谢

onLocationUpdate() just check for Internet is available or not,

If Internet is not available then just,

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

so like this,

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

EDIT: Here you can find beautiful explanation about Hoe to get current location,

What is the simplest and most robust way to get the user's current location in Android?

please refer this also.

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文