0

ここで尋ねられた質問と同様の問題があります: Sending message to a Handler on a dead thread when gets 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)

定期運行

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();
            }
        }
    }
}
4

1 に答える 1

0

更新:正確にはわかりませんが、onHandleIntent() の完了後に破棄されるため、別のオプションを使用して LocationListener を IntentService から移動したと思います。

このコードを正しく書く方法がわからないことに気付いたので、必要なことを実行するライブラリを探しに行きました。ここが出発点です。便利だと思ったのでブックマークしました:Androidでユーザーの場所を取得する良い方法

于 2014-01-30T20:14:38.983 に答える