在不使用gps或互联网的情况下获取用户的当前位置名称,但在android中使用Network_Provider [英] Get current location name of user without using gps or internet but by using Network_Provider in android

查看:206
本文介绍了在不使用gps或互联网的情况下获取用户的当前位置名称,但在android中使用Network_Provider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与 Android:获取用户的当前位置,而不使用gps或互联网接受的答案实际上没有回答问题。

This question is directly related to the same prevailing stackoverflow question at "Android: get current location of user without using gps or internet" where the accepted answer is actually not answering the question.

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

I should be able to get the current location name (eg:city name, village name) of the device via network provider not with GPS or internet. Following is the accepted answer in that question. (The following code parts should be included in the onCreate() method)

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






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


I changed the above code given in the linked answer as following but no success.

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

}

如何实现我的目标纠正上面的代码或任何其他方法?请注意,我想获取位置名称,但不是经度和纬度。有人可以帮助我。

How to accomplish what I want by correcting above code or any other method? Note that I want to get the location name, but not the longitudes and latitudes. Can somebody please help me.

推荐答案

你在这里所指的是什么(在旧手机上显示位置名称)是使用Cell完成的广播(或CB)。这与Location API或其任何变体完全无关。

What you are referring to here (showing location name on older phones) is done using "Cell Broadcast" (or "CB"). This has absolutely nothing to do with the Location API or any variations on that.

Cell tower可以发送设备可以接收的广播信息(类似于很多短信)。一些运营商已使用小区广播来广播小区塔所在位置的名称。一些运营商已经使用小区广播来广播小区塔的位置(纬度/经度)。一些运营商使用Cell Broadcast发送广告代码。 CB广播消息中包含的信息没有标准,每个移动运营商都可以选择是否使用此信息。

Cell towers can send out broadcast information that can be received by devices (something like "one to many SMS"). Some operators have used Cell Broadcast to broadcast the name of the location where the cell tower is. Some operators have used Cell Broadcast to broadcast the location (lat/long) of the cell tower. Some operators have used Cell Broadcast to send advertising tickers. There are no standards for the information contained in a CB broadcast message and each mobile operator can choose to use this or not.

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

Since most operators do not send these messages, it probably doesn't make sense to invest any time in trying to receive and decode them. But if you want to try, you can register a BroadcastReceiver listening for this Intent action: android.provider.Telephony.SMS_CB_RECEIVED. See the documentation for more details about what data is contained in the Intent.

这篇关于在不使用gps或互联网的情况下获取用户的当前位置名称,但在android中使用Network_Provider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆