Android locationManager - “location not changed”事件

时间:2011-12-09 22:11:21

标签: android android-service android-location

我正在建立一项服务,让我知道该位置在一段时间内是否有一段时间没有变化。

然后事情就是我的监听器上有事件onLocationChanged ..但我不知道如何做相反的事情......也就是说,如果位置在我提供的距离之内,则发送广播分钟。

这是我到目前为止的代码

LocationService

public class LocationService extends Service {

    public static final String LOC_INTENT = "com.xxx.intent.action.LOCATION";

    private Thread triggerService;
    protected LocationManager locationManager;
    protected MyLocationListener MyLocationListener;
    protected Criteria criteria;

    public static final int MIN_TIME = 300000; // 5 Minutes
    public static final long MIN_DISTANCE_MOTOR = 50; // 50 Metres

    private SharedPreferences settings;


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {

        settings = getSharedPreferences(getString(R.string.settings_prefsName), 0);
        addLocationListener();

        return START_STICKY;
    }

    private void addLocationListener()
    {
        triggerService = new Thread(new Runnable(){
            public void run(){
                try{
                    Looper.prepare();//Initialise the current thread as a looper.
                    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

                    criteria = new Criteria();
                    criteria.setAccuracy(Criteria.ACCURACY_FINE);

                    final String PROVIDER = locationManager.getBestProvider(criteria, true);

                    updateLocation(getLastBestLocation(MIN_TIME, MIN_DISTANCE_MOTOR));

                    MyLocationListener = new MyLocationListener();
                    locationManager.requestLocationUpdates(PROVIDER, MIN_TIME, MIN_DISTANCE_MOTOR, MyLocationListener);
                    Log.d("LOC_SERVICE", "Service RUNNING! ("+PROVIDER+")");
                    Looper.loop();
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }, "LocationThread");
        triggerService.start();
    }


    public Location getLastBestLocation(int minDistance, long minTime) {
        Location bestResult = null;
        float bestAccuracy = Float.MAX_VALUE;
        long bestTime = Long.MIN_VALUE;

        List<String> matchingProviders = locationManager.getAllProviders();
        for (String provider: matchingProviders) {
            Location location = locationManager.getLastKnownLocation(provider);
            if (location != null) {
                float accuracy = location.getAccuracy();
                long time = location.getTime();

                if ((time > minTime && accuracy < bestAccuracy)) {
                    bestResult = location;
                    bestAccuracy = accuracy;
                    bestTime = time;
                }
                else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) {
                    bestResult = location;
                    bestTime = time;
                }
            }
        }

        return bestResult;
    }

    public static void updateLocation(Location location)
    {
        Context appCtx = MyApplication.getAppContext();

        double latitude, longitude;
        float speed;

        latitude = location.getLatitude();
        longitude = location.getLongitude();
        speed = location.getSpeed();

        Intent filterRes = new Intent();
        filterRes.setAction(LOC_INTENT);
        filterRes.putExtra("latitude", latitude);
        filterRes.putExtra("longitude", longitude);
        filterRes.putExtra("speed", speed);
        appCtx.sendBroadcast(filterRes);
    }


    class MyLocationListener implements LocationListener
    {

        @Override
        public void onLocationChanged(Location location)
        {
            if(settings.getBoolean("active", false))
                updateLocation(location);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

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

}

1 个答案:

答案 0 :(得分:1)

设置一个计时器,无论你想测试多长时间。当它关闭时,检查onLocationChanged中的最后一个位置是否早于计时器长度。

修改

以下是我想象您的服务的方式

服务开始

服务正在运行

  • 在计时器关闭或调用onLocationChanged
  • 时执行必要的操作

服务停止