android - 位置管理器 requestLocationUpdates 只调用一次

标签 android location

我正在调用该方法并期望位置更新多次:

locationManager.requestLocationUpdates("gps",0 ,0, loc_listener);

我的 loc_listener 定义为:

LocationListener loc_listener = new LocationListener() {
    private final String TAG = "xoxoxo.LocationListener";

    public void onLocationChanged(Location l) {
        Intent locationAlert = new Intent("xoxoxo.LOCATION_CHANGED")
                .putExtra("target_location", l);
        sendBroadcast(locationAlert);
        // locationManager.requestLocationUpdates("gps", 0 ,0, this);
    }

    public void onProviderEnabled(String p) {
        Log.i(TAG, "Provider enabled");
    }

    public void onProviderDisabled(String p) {
        Log.i(TAG, "Provider disabled");
    }

    public void onStatusChanged(String p, int status, Bundle extras) {
        Log.i(TAG, "Status changed");
    }
};

按原样定义,我只会在 HTC Evo 2.2 和 2.2 + Google API 模拟器上获得一次更新。 获得多个更新的技巧是在每次更新时取消注释注册更新的行:

locationManager.requestLocationUpdates("gps", 0 ,0, this);

你们见过这样的事情吗?

最佳答案

我从来没有见过这个问题。以下是我测试 LocationManager 和 LocationListener 的代码。当 LocationListener 作为匿名类实现时,它按预期工作。

package com.test.locationmanager;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class LocationManagerStatus extends Activity {

    private LocationManager locationManager;
    private TextView textView;
    private final LocationListener gpsLocationListener =new LocationListener(){

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            final String tvTxt = textView.getText().toString();
            switch (status) {
            case LocationProvider.AVAILABLE:
                textView.setText(tvTxt + "GPS available again\n");
                break;
            case LocationProvider.OUT_OF_SERVICE:
                textView.setText(tvTxt + "GPS out of service\n");
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                textView.setText(tvTxt + "GPS temporarily unavailable\n");
                break;
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "GPS Provider Enabled\n");
        }

        @Override
        public void onProviderDisabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "GPS Provider Disabled\n");
        }

        @Override
        public void onLocationChanged(Location location) {
            locationManager.removeUpdates(networkLocationListener);
            textView.setText(textView.getText().toString()
                    + "New GPS location: "
                    + String.format("%9.6f", location.getLatitude()) + ", "
                    + String.format("%9.6f", location.getLongitude()) + "\n");
        }
    };
    private final LocationListener networkLocationListener =
                                                        new LocationListener(){

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras){
            final String tvTxt = textView.getText().toString();
            switch (status) {
            case LocationProvider.AVAILABLE:
                textView.setText(tvTxt + "Network location available again\n");
                break;
            case LocationProvider.OUT_OF_SERVICE:
                textView.setText(tvTxt + "Network location out of service\n");
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                textView.setText(tvTxt
                        + "Network location temporarily unavailable\n");
                break;
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "Network Provider Enabled\n");
        }

        @Override
        public void onProviderDisabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "Network Provider Disabled\n");
        }

        @Override
        public void onLocationChanged(Location location) {
            textView.setText(textView.getText().toString()
                    + "New network location: "
                    + String.format("%9.6f", location.getLatitude()) + ", "
                    + String.format("%9.6f", location.getLongitude()) + "\n");
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.textview);
        locationManager = (LocationManager) 
                getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 5000, 0,
                networkLocationListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                3000, 0, gpsLocationListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(networkLocationListener);
        locationManager.removeUpdates(gpsLocationListener);
    }
}

关于android - 位置管理器 requestLocationUpdates 只调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4772686/

相关文章:

android - AdvertisingIdClient : Error while reading from SharedPreferences java. lang.SecurityException:不再支持 MODE_WORLD_READABLE

java - java swing中负坐标绘图

android - MapView Activity 关闭后,我是否需要停止使用 GPS?

android - 可以直接跳转到 ViewAnimator 中的第二个 View

Android TextView 在 Jelly Bean 上不可见

android - Android ICS 上的 GCM 通知不起作用

ios - 通过 iPhone 的 IP 地址进行地理定位

java - 无法使用 RecyclerAdapter 将对象转换为字符串值

android - 选择性 Google Play 服务 API 找不到类

ios - 在 iOS (Swift) 中使用后台位置更新的最佳方式