2011-12-09 15 views
0

一定の時間内に場所が変更されていないかどうかを知らせるサービスを構築しています。Android locationManager - "場所が変更されていません"イベント

私のリスナーには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) { 
     } 
    } 

} 
+0

あなたは 'requestLocationUpdates'を一度呼び出すだけで、場所の更新を受け取るためのすべてのロジックは' onLocationChanged'になければなりません。 – Craigy

答えて

1

私が持っているコードです。オフになったら、最後に取得した場所がonLocationChangedであるかどうかをタイマーの長さよりも古いかどうかを確認します。

EDITここ

は、私はあなたのサービスを見て想像する方法です

サービス開始あなたは 後に通知されるということを、適切な最小の時間と最短距離で呼び出さ

あなたのタイマーがオフになるかonLocationChangedとき

  • を実行しているサービスは、必要なアクションを実行するにはremoveUpdates
    • 削除位置情報の更新を停止

    サービスがあなたを停止すると呼ばれています時間r

+0

私はonFinishメソッド内に新しいタイマーを作成するとよいでしょうか?サービスが実行されている間はループするでしょうか? – SERPRO

+0

私は 'onFinish'メソッドとは何ですか?私は輪郭で私の答えを更新します – Craigy

+0

申し訳ありません、私は 'Timer'の代わりに' CountDownTimer'を使っていました。よく働く。ありがとう! – SERPRO

関連する問題