【问题标题】:How to get current gps location both online or offline in android?如何在android中在线或离线获取当前gps位置?
【发布时间】:2014-09-02 18:56:26
【问题描述】:

我想让当前位置同时使用在线或离线 GPS 以及 GPS 的预期行为是: 1.如果wifi或3g网络可用,它将使用它们来获取GPS位置。 2. 如果没有 wifi 或 3g 网络可用,它将使用平板电脑的离线 GPS(如果存在) 3. 如果什么都没有,它会显示错误信息。

我尝试了很多但还没有成功。我总是启用网络。

public boolean isGPSEnabled() {
        Location location = null;
        try {
            mLocationManager = (LocationManager) getApplicationContext()
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = mLocationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = mLocationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
                return false;
            }
        } catch (Exception e) {
            toast("Exception " + e.toString());
        }
        return true;
    }

如果有人有想法。请帮忙。提前致谢

【问题讨论】:

    标签: android gps location


    【解决方案1】:

    不是单个函数,而是将其拆分为两个函数来检查哪个函数被启用

    检查 GPS 是否启用

    public boolean isGPSEnabled() {
        Location location = null;
        try {
            mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
            // getting GPS status
            isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception e) {
            toast("Exception " + e.toString());
        }
        return true;
    }
    

    检查WIFI是否开启

    public boolean isWIFIEnabled() {
        Location location = null;
        try {
            mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
            // getting network status
            isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception e) {
            toast("Exception " + e.toString());
        }
        return true;
    }
    

    那么当你想检查它时,试试这个

    if(isWIFIEnabled())
    {
        //request location updates from network provider
    }
    else if(isGPSEnabled())
    {
        //request location updates from gps provider    
    }
    else
    {
        //display error
    }
    

    【讨论】:

    • 非常感谢您的回复。但问题是即使 gps 关闭,isGPSEnabled 总是返回 true。
    • 那么我认为你将不得不使用广播接收器来检查 GPS 是否得到修复..
    • 有没有链接。我没有确切的想法。如何修复它。谢谢!
    • 感谢您的回复。没有解决。每次启用wifi都是真的。可能是设备问题。所以,将在另一台设备上进行测试。
    • 是的...可能是您的设备有问题...请更新我..@jagdish
    猜你喜欢
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 2014-05-09
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多