在不使用 GPS 或互联网的情况下获取用户在 Andr

Get current location of user in Android without using GPS or internet(在不使用 GPS 或互联网的情况下获取用户在 Android 中的当前位置)
本文介绍了在不使用 GPS 或互联网的情况下获取用户在 Android 中的当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不使用 GPS 或互联网的情况下获取用户的当前位置?我的意思是在移动网络提供商的帮助下.

Is it possible to get the current location of user without using GPS or the internet? I mean with the help of mobile network provider.

推荐答案

你要做的是使用 LocationManager.NETWORK_PROVIDER 而不是 LocationManager.GPS_PROVIDER.NETWORK_PROVIDER 将在可用的 GSM 或 wifi 上解析.显然,在关闭 wifi 的情况下,将使用 GSM.请记住,使用蜂窝网络基本上可以精确到 500m.

What you are looking to do is get the position using the LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER. The NETWORK_PROVIDER will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. Keep in mind that using the cell network is accurate to basically 500m.

http://developer.android.com/guide/topics/location/obtaining-user-location.html 有一些非常棒的信息和示例代码.

http://developer.android.com/guide/topics/location/obtaining-user-location.html has some really great information and sample code.

在完成OnCreate() 中的大部分代码后,添加以下代码:

After you get done with most of the code in OnCreate(), add this:

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      makeUseOfNewLocation(location);
    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

您还可以让您的活动实现 LocationListener 类,从而在您的活动中实现 onLocationChanged().

You could also have your activity implement the LocationListener class and thus implement onLocationChanged() in your activity.

这篇关于在不使用 GPS 或互联网的情况下获取用户在 Android 中的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)