在不使用GPS或互联网的情况下获取用户的当前位置名称,而是通过在Android中使用Network_Provider

萨米塔·查图兰加(Samitha Chathuranga)

此问题与“ Android:无需使用gps或Internet即可获取用户的当前位置”中相同的通用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);

我更改了链接答案中给出的以上代码,如下所示,但未成功。

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

}

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

大卫·水

您在此处指的是(在较旧的电话上显示位置名称)是使用“小区广播”(或“ CB”)完成的。这与Location API或它的任何变化绝对无关。

蜂窝基站可以发出设备可以接收的广播信息(例如“一对多SMS”)。一些运营商已经使用“小区广播”来广播基站位置的名称。一些运营商已经使用“小区广播”来广播小区信号塔的位置(经/纬度)。一些运营商已使用“小区广播”发送广告报价。CB广播消息中包含的信息没有标准,每个移动运营商都可以选择使用还是不使用。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档