Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 来自意向服务的位置更新(永远不会调用onLocationChanged)_Android_Intentservice_Locationlistener - Fatal编程技术网

Android 来自意向服务的位置更新(永远不会调用onLocationChanged)

Android 来自意向服务的位置更新(永远不会调用onLocationChanged),android,intentservice,locationlistener,Android,Intentservice,Locationlistener,我的问题和这个一样,, 但不幸的是,我无法理解这一点,我从android设备获取位置的代码在活动中运行良好,但当涉及到intent服务时,onLocationChanged()方法永远不会被调用 该服务的其他部分运行良好,因为我也在同一服务中实现了notification manager示例,以跟踪各种变量的值,但是由onLocationChanged()修改的变量永远不会被修改,这表明该方法没有被执行。 请帮助据我所知,位置传感器需要在UI线程中运行。活动在那里运行,但服务在后台运行 在代码中

我的问题和这个一样,, 但不幸的是,我无法理解这一点,我从android设备获取位置的代码在活动中运行良好,但当涉及到intent服务时,onLocationChanged()方法永远不会被调用

该服务的其他部分运行良好,因为我也在同一服务中实现了notification manager示例,以跟踪各种变量的值,但是由onLocationChanged()修改的变量永远不会被修改,这表明该方法没有被执行。
请帮助

据我所知,位置传感器需要在UI线程中运行。活动在那里运行,但服务在后台运行

在代码中添加一些密集的日志记录和异常处理,以了解情况


如果这是原因,那么有一种方法可以创建并注册
活套
。让我知道,我可以在我的代码中搜索,IntentService不会等待您的onLocationChangedListener,如果IntentService的最后一部分是注销您的location changed listener,那么这就是您的问题所在

您可以做的是将您的IntentService转换为常规服务。检查不同的操作,其中一个是接收新位置时的下一步

i、 e

为你效劳

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent!=null) {
        String action = intent.getAction();
            if (action!= null) {
                if (action.equals("LOCATION_RECEIVED")) {
                    Location location = null;
                    if (intent.hasExtra("locationUpdate")) {
                        location = intent.getExtras().getParcelable("locationUpdate");
                        //Process new location updates here
                    }
另一种方法是在LocationListener上使用Pending intent,但效果与上述代码相同。第三种方法是从OnLocationChangedListener向您的IntentService发布runnable(我个人还没有尝试过这一种)


如果你能在这里分享一些代码,我将不胜感激。如果你还有其他问题。我可能会有所帮助,因为我正在从事一个类似的项目,从服务中获取位置。

我在stackoverflow上的某个地方读到,我的代码可能太快,可能发生onLocationChanged()方法从未返回,因此服务在返回之前就被销毁,但我已经得到了所需的,我从getLastKnownLocation()检索到了最后一次位置更新,现在我的应用程序运行正常,谢谢!
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent!=null) {
        String action = intent.getAction();
            if (action!= null) {
                if (action.equals("LOCATION_RECEIVED")) {
                    Location location = null;
                    if (intent.hasExtra("locationUpdate")) {
                        location = intent.getExtras().getParcelable("locationUpdate");
                        //Process new location updates here
                    }