【问题标题】:Android onLocationChanged not calledAndroid onLocationChanged 未调用
【发布时间】:2023-03-13 16:45:01
【问题描述】:

我需要做什么才能让我的 LocationListener 触发它的 onLocationChanged 方法?

我是这个东西的新手(java 和 android)。这就是我正在做的事情。 我通过调用 context.startService(serviceIntent); 来处理位置请求。 在意图服务中,我有一个嵌套类来充当 LocationListener 在 onHandleIntent 方法中,我启动了一个请求位置更新的线程 我在 OnHandleIntent 方法中维护了对 LocationListener 对象和踏板的引用。

我的计划是让 onLocationChanged 方法更新一个变量,我可以定期检查以查看更新是否已完成,然后跳出循环并对该位置执行一些操作。

问题是永远不会调用 onLocationChanged 方法。 LocationListener 对象在 onHandleIntent 方法结束之前仍然有效。 我什至可以直接调用它并获得我期望的调试消息。 我很确定 DDMS 正在更改位置,因为我已将其打印在各种调试日志中并且我看到它正在更改。

代码如下:

package com.example.locationrequestreceiver;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;

public class LocationRequestService extends IntentService    {


    private LocationManager locationManager;

    public class UpdateHandler implements Runnable, LocationListener {

        @Override
        public void onLocationChanged(Location arg0) {
            Log.d("myStuff", "inside location changed");
        }

        @Override
        public void onProviderEnabled(String provider) {}
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        @Override
        public void onProviderDisabled(String provider) {}

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Looper.prepare();
            Log.d("myStuff", "inside thread");
            locationManager.requestLocationUpdates("gps", 0, 0, this);
            Log.d("myStuff", "run ending");
            Looper.loop()  //  <--- this was the fix
        }

    }

    public LocationRequestService() {
        super("LocationRequrestService");
        Log.d("myStuff", "inside the service");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        UpdateHandler update_handler = new UpdateHandler();
        Thread update_handler_thread = new Thread(update_handler);
        Log.d("myStuff", "starting thread");
        update_handler_thread.start();

        for (int count = 0; count < 20; count++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
        Location location = locationManager.getLastKnownLocation("gps");
        update_handler.onLocationChanged(location);
    }




}

【问题讨论】:

    标签: android locationmanager


    【解决方案1】:

    调用线程必须有一个Looper。您可以使用HandlerThread 代替线程。

    【讨论】:

    • 你能详细说明一下哦,这里怎么做。我在 HandlerThread 上阅读了文档和一些东西,但不清楚如何在这里使用它。
    • BTW ...这确实给了我在我的线程运行方法中调用 Looper.loop() 的提示,但我想尝试让 HandlerThread 工作
    • 一个更简单的解决方案就是在主 UI 线程上运行 LocationListener。位置更新本质上是异步的,因此它们不会阻塞您的 UI 线程。有什么理由必须在单独的线程上运行?
    • 请求来自广播接收器。我的理解是广播接收器可能(或确实)在 onReceive 完成后立即消失。这样就创建/启动了 intentService,我最初尝试将其用作侦听器,但似乎也没有真正起作用,所以我想我可以启动一个线程,该线程应该等待足够长的时间来接收更新。
    • 我将此标记为已回答,并将 Looper.loop() 语句添加到我的代码中。谢谢阿什温。
    猜你喜欢
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    相关资源
    最近更新 更多