【问题标题】:How to get locationchaged using Intentservice?如何使用 Intentservice 获取位置信息?
【发布时间】:2013-04-23 02:50:11
【问题描述】:

我想在位置更改时获取纬度和经度。如何使用 Intentservice?另一件事是即使应用程序在后台我也想要当前的纬度和经度。

【问题讨论】:

    标签: android service background android-pendingintent intentservice


    【解决方案1】:

    您可以尝试getLastKnownLocation(),但它很可能为空。并且 IntentService 不能等待某个位置修复到达。

    相反,您需要一个常规服务来处理这种情况。改用cwac-locpoll

    【讨论】:

    • 所以你的意思是,如果我想要连续的位置,我必须使用服务而不是 Intentservice?
    【解决方案2】:

    试试下面的代码,它对我有用以获得坐标

    public Location getLocation() {
            try {
                locationManager = (LocationManager) mContext
                        .getSystemService(LOCATION_SERVICE);
            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
    
            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return location;
    }
    

    希望对你也有用

    【讨论】:

    • 这在IntentService 中无法正常工作,因为在您获得位置修复之前很久,该服务就会被破坏。
    • @CommonsWare 如何连续使用 IntentService 获取 CurrentLocation?请提供一些指导方针。谢谢
    • @nitishpatel:我已经对 anilMotwani 的答案投了赞成票,这是正确的答案。
    【解决方案3】:

    您需要在服务中实现 LocationListner。这是一个包含更多信息的链接。 Location Listener in Background Service AndroidLocation listener in a service sends notification issue

    如果您不想使用服务: BroadcastReceiver for location 您需要实现自定义广播接收器。

    【讨论】:

    • 我不想使用服务,因为它会耗尽电池电量。我怎样才能获得与您建议我使用服务相同的结果,但我希望使用 Intentservice 获得相同的结果。是否有可能获得相同的结果如服务所给?
    【解决方案4】:

    如果你真的必须使用 IntentService,你可以实现一个带有适当超时的循环。使用 onLocationChanged 更改一些值。 循环可以每隔一秒测试一次这个值。如果值改变,退出循环,如果达到超时退出循环。 该循环将使 IntentService 保持活动状态,等待 onLocationChanged 触发。

    【讨论】:

    • 在这里找到了更好的方法stackoverflow.com/questions/6866415/…
    • 我试过这个答案,但 onlocationchanged 从未被解雇,但如果你在片段或活动中执行相同的代码,它确实有效,我相信你不能在 IntentService 中使用这个“技巧”。
    • @angel,使用新线程获取位置,然后使用 thread.join(10000);
    • @angel,使用新线程获取位置,然后使用 thread.join(10000);将当前线程与新线程加入。创建一个回调方法,将在 onlocationchanged 中使用以捕获当前位置属性
    猜你喜欢
    • 2013-05-10
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2011-12-29
    相关资源
    最近更新 更多