在不使用gps或互联网的情况下获取用户的当前位置名称,但在android中使用Network_Provider

Sam*_*nga 13 java android location locationmanager

这个问题与" Android:获取用户的当前位置而不使用gps或互联网 " 相同的主流stackoverflow问题直接相关,其中接受的答案实际上没有回答问题.

我应该能够通过网络提供商而不是GPS或互联网获取设备的当前位置名称(例如:城市名称,村庄名称).以下是该问题的公认答案.(以下代码部分应包含在onCreate()方法中)

// 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);
Run Code Online (Sandbox Code Playgroud)

我更改了链接答案中给出的上述代码如下,但没有成功.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView txtView = (TextView) findViewById(R.id.tv1);
    txtView.setText("ayyo samitha");
    ////

    // Acquire a reference to the system Location Manager
    LocationManager 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);

        }

        private void makeUseOfNewLocation(Location location) {
            txtView.setText("sam came in");
            txtView.append(location.toString());
        }

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

        public void onProviderEnabled(String provider) {
           // makeUseOfNewLocation(location);
        }

        public void onProviderDisabled(String provider) {}
    };

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

}
Run Code Online (Sandbox Code Playgroud)

如何通过纠正上面的代码或任何其他方法来完成我想要的?请注意,我想获取位置名称,但不是经度和纬度.有人能帮帮我吗.

Dav*_*ser 4

您在这里所指的内容(在旧手机上显示位置名称)是使用“小区广播”(或“CB”)完成的。这与 Location API 或其任何变体完全无关。

蜂窝塔可以发送设备可以接收的广播信息(例如“一对多短信”)。一些运营商已经使用小区广播来广播手机信号塔所在位置的名称。一些运营商已使用小区广播来广播手机信号塔的位置(纬度/经度)。一些运营商已经使用小区广播来发送广告行情。CB 广播消息中包含的信息没有标准,每个移动运营商可以选择使用或不使用它。

由于大多数运营商不发送这些消息,因此投入任何时间尝试接收和解码它们可能没有意义。但如果您想尝试,您可以注册BroadcastReceiverIntent操作的监听:android.provider.Telephony.SMS_CB_RECEIVED。有关 .txt 文件中包含哪些数据的更多详细信息,请参阅文档Intent