在IntentService检查之前唤醒GPS和网络定位器

时间:2013-10-23 01:51:02

标签: android geolocation locationmanager

我对这里提出的问题有类似的问题: Sending message to a Handler on a dead thread when getting a location from an IntentService

我把一个LocationListener放在一个IntentService中,我听说这是一个不好的地方,因为在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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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