【问题标题】:Wake up GPS and Network locator before it's checked by an IntentService在 IntentService 检查之前唤醒 GPS 和网络定位器
【发布时间】:2013-10-23 01:51:02
【问题描述】:

我遇到了与此处提出的问题类似的问题: Sending message to a Handler on a dead thread when getting a location from an IntentService

我在 IntentService 中放置了一个 LocationListener,我听说这是一个不好的放置位置,因为在 onHandleIntent() 完成后监听器会被销毁。

我的意图服务会根据 GPS 和网络提供商检查最后一个已知位置。我正在寻找一种在检查用户所在位置之前“唤醒” GPS 和网络定位器的方法。

IntentService 每分钟运行一次,它每 5-30 分钟检查一次更新的位置。有没有办法设置我可以在实际位置检查之前调用的requestLocationUpdates

12-31 14:09:33.664: W/MessageQueue(3264): Handler (android.location.LocationManager$ListenerTransport$1) {41403330} sending message to a      Handler on a dead thread
12-31 14:09:33.664: W/MessageQueue(3264): java.lang.RuntimeException: Handler (android.location.LocationManager$ListenerTransport$1) {41403330} sending message to a Handler on a dead thread
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessage(Handler.java:383)
12-31 14:09:33.664: W/MessageQueue(3264): at android.location.LocationManager$ListenerTransport.onLocationChanged(LocationManager.java:193)
12-31 14:09:33.664: W/MessageQueue(3264): at android.location.ILocationListener$Stub.onTransact(ILocationListener.java:58)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Binder.execTransact(Binder.java:338)
12-31 14:09:33.664: W/MessageQueue(3264): at dalvik.system.NativeStart.run(Native Method)

ScheduledService

public class ScheduledService extends IntentService {

    private LocationManager lm;
    LocationListener locationListener;

    public ScheduledService() {
        super("ScheduledService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                lm.removeUpdates(this);
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
        };

        if(isNetworkAvailable()) {

            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

            try {
                Location lastKnownLocation = getBestLocation();
                double lat = lastKnownLocation.getLatitude();
                double lon = lastKnownLocation.getLongitude();

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

【问题讨论】:

  • 您找到解决方案了吗?如果可以,你能回答你自己的问题吗?
  • 是的,抱歉,我通常会重点更新我的问题。我会发布一个答案。

标签: android geolocation locationmanager


【解决方案1】:

更新:我不太确定,但我相信我选择了不同的选项并将 LocationListener 移出 IntentService,因为它在 onHandleIntent() 完成后被销毁。

我刚刚意识到我不知道如何正确编写这段代码,所以我去寻找一个可以满足我需要的库。这是一个起点。我收藏了它,因为我发现它很有用:Good way of getting the user's location in Android

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多