2011-12-02 121 views
4

我想在特定時間間隔(比如說30分鐘)後捕獲android設備的當前位置(緯度和經度)..我的課程(或服務??不知道要使用什麼)當設備引導完成時到LocationManagerListener。什麼是實施這個最好的方法?我們如何在這種情況下使用locationChanged()方法?在特定時間間隔後獲取當前設備位置

這是我認爲它可以去:

監聽啓動完成的事件,並設置報警服務:

public class OnBootReceiver extends BroadcastReceiver { 
    private static final int PERIOD=1800000; // 30 minutes 

    @Override 
    public void onReceive(Context context, Intent intent) { 

    AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent i=new Intent(context, OnAlarmReceiver.class); 
    PendingIntent pi=PendingIntent.getBroadcast(context, 0, 
               i, 0); 

    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
         SystemClock.elapsedRealtime()+60000, 
         PERIOD, 
         pi); 
    } 
} 

監聽報警服務,並啓動位置捕獲類或服務:

public class OnAlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) {  
     WakefulIntentService.acquireStaticLock(context); 
     context.startService(new Intent(context, locationCapture.class)); 
     or 
     new locationCapture().classmethod(); 
    } 
    } 

我不知道locationCapture類應該如何實現。它應該是普通的Java類還是Service類?

任何幫助將不勝感激。

回答

6

此服務類,你可以用它

public class ServiceLocation extends Service{ 
    private LocationManager locMan; 
    private Boolean locationChanged; 
    private Handler handler = new Handler(); 

    public static Location curLocation; 
    public static boolean isService = true; 

    LocationListener gpsListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      if (curLocation == null) { 
       curLocation = location; 
       locationChanged = true; 
      }else if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude()){ 
       locationChanged = false; 
       return; 
      }else 
       locationChanged = true; 

      curLocation = location; 

      if (locationChanged) 
       locMan.removeUpdates(gpsListener); 

     } 
     public void onProviderDisabled(String provider) { 
     } 

     public void onProviderEnabled(String provider) { 
     } 

     public void onStatusChanged(String provider, int status,Bundle extras) { 
      if (status == 0)// UnAvailable 
      { 
      } else if (status == 1)// Trying to Connect 
      { 
      } else if (status == 2) {// Available 
      } 
     } 

    }; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     curLocation = getBestLocation(); 

     if (curLocation == null) 
      Toast.makeText(getBaseContext(),"Unable to get your location", Toast.LENGTH_SHORT).show(); 
     else{ 
      //Toast.makeText(getBaseContext(), curLocation.toString(), Toast.LENGTH_LONG).show(); 
     } 

     isService = true; 
    } 
    final String TAG="LocationService"; 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    return super.onStartCommand(intent, flags, startId); 
    } 
    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
    } 

    @Override 
    public void onStart(Intent i, int startId){ 
     handler.postDelayed(GpsFinder,1); 
    } 

    @Override 
    public void onDestroy() { 
     handler.removeCallbacks(GpsFinder); 
     handler = null; 
     Toast.makeText(this, "Stop services", Toast.LENGTH_SHORT).show(); 
     isService = false; 
    } 

    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

    public Runnable GpsFinder = new Runnable(){ 
    public void run(){ 

     Location tempLoc = getBestLocation(); 
     if(tempLoc!=null) 
      curLocation = tempLoc; 
     tempLoc = null; 
     handler.postDelayed(GpsFinder,1000);// register again to start after 1 seconds...   
    } 
    }; 

    private Location getBestLocation() { 
     Location gpslocation = null; 
     Location networkLocation = null; 

     if(locMan==null){ 
      locMan = (LocationManager) getApplicationContext() .getSystemService(Context.LOCATION_SERVICE); 
     } 
     try { 
      if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
       locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 1, gpsListener);// here you can set the 2nd argument time interval also that after how much time it will get the gps location 
       gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

      } 
      if(locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ 
       locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 1, gpsListener); 
       networkLocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      } 
     } catch (IllegalArgumentException e) { 
      //Log.e(ErrorCode.ILLEGALARGUMENTERROR, e.toString()); 
      Log.e("error", e.toString()); 
     } 
     if(gpslocation==null && networkLocation==null) 
      return null; 

     if(gpslocation!=null && networkLocation!=null){ 
      if(gpslocation.getTime() < networkLocation.getTime()){ 
       gpslocation = null; 
       return networkLocation; 
      }else{ 
       networkLocation = null; 
       return gpslocation; 
      } 
     } 
     if (gpslocation == null) { 
      return networkLocation; 
     } 
     if (networkLocation == null) { 
      return gpslocation; 
     } 
     return null; 
    } 
} 

您可以設置一次進入處理程序或進入requestLocationUpdates()。你需要從家裏開始這項服務。由於我已經在處理程序和requestLocationUpdate()中設置了1秒以獲得1秒後的位置並更新我。

編輯: 由於此服務獲取用戶的當前位置,您可以從家庭活動開始,也可以從啓動時開始。有了家庭活動,如果服務被用戶使用另一個應用程序(如任務殺手)停止,那麼當用戶啓動應用程序時,然後從家中重新啓動該服務,啓動服務,您可以這樣做

startService(new Intent(YourActivity.this,ServiceLocation.class)); 

當您需要停止服務時,它會調用onDestroy(),以便處理程序可以取消線程以繼續獲取位置。

由於GPS設置爲1秒(1000),這將每1秒獲得gps位置,但爲了獲取該位置,您需要每次打電話,在我的情況下,我已將其設置爲1秒,並根據您的要求設置爲30秒。所以你每1秒獲得一次位置,並在處理器中使用這個服務設置30分鐘。爲了節省電池使用時間,您還可以設置不同的時間到GPS請求方法,以便節省電池壽命。

您可以刪除位置和當前位置的比較部分,因爲每當位置發生變化時,每次調用locationlistener時,都可以在應用程序中的任意位置使用curLocation來獲取當前位置,但請確保首先必須開始此操作服務第一,然後後就可以使用,否則你得到空指針異常,當你進入此對象的緯度和經度

希望你有一些想法,我得到的回答你的疑問

+0

嘿pratik - 非常感謝您的時間。我有幾個疑問 - – Swati

+0

好吧問問題。你有什麼疑問? – Pratik

+0

嘿pratik - 非常感謝您的時間。我有幾個疑問 - 1)你建議這個服務在OnAlarmReceiver類或直接在OnBootReceiver類下啓動(請參考上面的代碼)2)何時和誰將調用此服務的OnDestroy()? 3)爲什麼我們在每隔1秒後在代碼處理程序.postDelayed(GpsFinder,1000)之後得到「最後的位置」... requestLocationUpdates()會在每隔一秒鐘後調用OnLocationChanged(),我們將比較舊位置和當前位置這不夠嗎? – Swati