如何让 Android LocationManager 使用 Cell 网络而不是 Wifi 网络?

发布于 2024-09-28 07:38:10 字数 427 浏览 1 评论 0原文

我有一个使用 LocationManager 获取手机网络位置而不是 Wifi 位置的 Android 应用程序?如果我关闭了 Wifi 天线并执行以下操作:

LocationManager lm = (LocationManager) paramContext.getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

我总是返回 Wifi 位置,而不是蜂窝网络位置。我希望它返回手机位置,因为我已经离开 Wifi 位置。我尝试使用 LocationListener 但这似乎没有帮助。

这是在运行 Froyo 的 HTC Evo 上运行的。

谢谢!

I have an android application using LocationManager get the cell network location and not the Wifi location? If I have turned off the Wifi antenna and do the following:

LocationManager lm = (LocationManager) paramContext.getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

I am always returned the Wifi location and not the cell network location. I want it to return the cell location since I have moved fromthe Wifi location. I tried using a LocationListener but that doesn't seem to help.

This is running on an HTC Evo running Froyo.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[浮城] 2024-10-05 07:38:10

我不相信你可以。

网络位置提供商会查看 wifi 状态和小区状态,如果太旧则丢弃其中一个,或者通过 MASF 服务器将它们发送到 Google 位置服务器 (GLS)。那里所做的事情很神奇。但是您可以查看 NetworkProvider 的实现并了解如何调整您的代码。代码在这里: 网络位置提供商

如果您只需要手机 ID,请查看电话管理器。在那里添加一个 PhoneStateListener

I don't believe you can.

Network location provider look at the wifi state and cell state, discard one if too old, or send them both over to the Google Location Server (GLS) via MASF server. What's done there is magic. But you can look at the implementation of NetworkProvider and see how to tweak your code. Codes are here: NetworkLocationProvider.

If you just want the cell ID, check out Telephony Manager. Add a PhoneStateListener there.

喜爱纠缠 2024-10-05 07:38:10

查看 Android 开发指南中的此部分:请求位置更新。基本上,您需要注册一个侦听器来获取位置更新,然后告诉 Android 您希望通过网络位置提供商获取这些更新。

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

然后,您应该能够使用 getLastKnownLocation() 获取最后一个已知位置和/或使用 LocationListener 中的 onLocationChanged() 。

Check out this section in the Android Dev Guide: Requesting Location Updates. Basically, you need to register a listener to get location updates, and then you tell Android that you want to get those updates with the network location provider.

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

You should then be able to get the last known location with getLastKnownLocation() and/or use the onLocationChanged() in the LocationListener.

躲猫猫 2024-10-05 07:38:10

我不知道你现在是否有答案,但是你是否尝试在网络信息上编写一些逻辑并获取你所在的网络类型?并拨打定位服务电话。

 public boolean haveNetworkConnection()
{
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo)
    {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}

I don't if you have an answer by now, but did you try to write some logic on network info and get type of network you are in ?? and do a location service call.

 public boolean haveNetworkConnection()
{
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo)
    {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文