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

2022-09-04 22:43:06

这个问题与“Android:在不使用GPS或互联网的情况下获取用户的当前位置”中相同的当前堆栈溢出问题直接相关,其中接受的答案实际上没有回答问题。

我应该能够通过网络提供商获取设备的当前位置名称(例如:城市名称,村庄名称),而不是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);
    }

}

如何通过更正上述代码或任何其他方法完成我想要的?请注意,我想获取位置名称,但不想获取经度和纬度。有人可以帮我吗?


答案 1

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

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

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


答案 2

问题是你尝试的代码确实有效,可能只是没有你希望的那么好。例如,这种方法在三星Galaxy S3上提供的精度为2000m,这意味着实际位置位于半径为2公里的圆圈内的任何地方。此外,由于误差幅度非常大,因此可能需要相当大的位置更改才能通知你的应用位置更改。

需要GPS或(如果使用Google Play服务)才能获得合理的好位置。这确实需要 ,但是除非您只要求公里级精度,否则此权限是必须的。LocationRequest.PRIORITY_BALANCED_POWER_ACCURACYandroid.permission.ACCESS_FINE_LOCATION

最后请注意,使用Google Play服务,我可以在不打开GPS的情况下获得精确到10米的位置数据,因此这仍然应该满足您的要求。LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY

下面是一个完整的示例:

AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

主要活动.java

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class MainActivity extends Activity implements
        com.google.android.gms.location.LocationListener, ConnectionCallbacks,
        OnConnectionFailedListener {
    private final FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
    private GoogleApiClient mGoogleAPIClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGoogleAPIClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API).addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();
    }

    @Override
    protected void onResume() {
        super.onResume();

        mGoogleAPIClient.connect();
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (mGoogleAPIClient != null) {
        mGoogleAPIClient.disconnect();
        }
    }

    @Override
    public void onConnected(Bundle arg0) {
        final LocationRequest locationRequest = LocationRequest.create();
        locationRequest
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        locationRequest.setInterval(30 * 1000);
        locationRequest.setFastestInterval(5 * 1000);
        fusedLocationProviderApi.requestLocationUpdates(mGoogleAPIClient,
                locationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChanged(Location location) {
        // the location is no more than 10 min old, and with reasonable
        // accurarcy (50m), done
        if (System.currentTimeMillis() < location.getTime() + 10 * 60 * 1000
                && location.getAccuracy() < 50) {
            mGoogleAPIClient.disconnect();
            mGoogleAPIClient = null;
            ((TextView) findViewById(R.id.test)).setText(location.toString());
        }
    }
}

推荐