2016-04-11 17 views
1

私はアンドロイドに新しいですし、アプリケーションが、それは関係なく、状態のサーバーに自分の現在位置を更新し続ける必要がありますquit.ieであっても、私の場所の更新を取得する機能を実現するためのアプリに取り組んで私のアプリケーションの - 終了のアクティブ。私は同じのためにどのようなアプローチに従ってくださいするようAndroidの位置情報の更新

は、あなたたちは私を提案することができます? が本当にあなたの助けいただければ幸いです...ここで

答えて

6

は、すべてのX分またはハンドラを使用して、すべてのXメートル、ユーザの位置を取得するためのコードです。

Location gpslocation = null; 

    private static final int GPS_TIME_INTERVAL = 60000; // get gps location every 1 min 
    private static final int GPS_DISTANCE= 1000; // set the distance value in meter 

    /* 
     for frequently getting current position then above object value set to 0 for both you will get continues location but it drown the battery 
    */ 

    private void obtainLocation(){ 
    if(locMan==null) 
     locMan = (LocationManager) getSystemService(LOCATION_SERVICE); 

     if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
      gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      if(isLocationListener){ 
       locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
          GPS_TIME_INTERVAL, GPS_DISTANCE, GPSListener); 
        } 
       } 
      } 
    } 

ハンドラ

private static final int HANDLER_DELAY = 1000*60*5; 

    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
      public void run() { 
       myLocation = obtainLocation(); 
       handler.postDelayed(this, HANDLER_DELAY); 
      } 
     }, START_HANDLER_DELAY); 

GPS LocationListener

private LocationListener GPSListener = new LocationListener(){ 
    public void onLocationChanged(Location location) { 
     // update your location 

    } 

    public void onProviderDisabled(String provider) { 
    } 

    public void onProviderEnabled(String provider) { 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
}; 
+0

コードのThanx、locMan.removeUpdates(GPSListener)を含むy r uについて説明できますか? 、私はあなたがリスナーを削除したら、それはどのように場所の変化をキャッチするのですか? mistake..iによりだった – lionheart

+0

はそれを編集しました – Hackose

4

これはバックグラウンドサービスで装置の緯度と経度を更新するためのコードであり、常にバックグラウンドで動作します。誰か(人的またはシステム)によって停止された場合、サービスは自動的に再開されません。

public class LocationService extends Service implements LocationListener { 

     LocationManager m_locationManager; 

     @Override 
     public void onCreate() { 

      this.m_locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

       Toast.makeText(getApplicationContext(), "Location Service starts", Toast.LENGTH_SHORT).show(); 

     } 



     @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 
      //TODO do something useful 
      Toast.makeText(getApplicationContext(), "Service starts", Toast.LENGTH_SHORT).show(); 



      // Here I offer two options: either you are using satellites or the Wi-Fi services to get user's location 
      // this.m_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0, this); // User's location is retrieve every 3 seconds 
      this.m_locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

      this.m_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this); 
     return START_STICKY; 
     } 


     @Override 
     public IBinder onBind(Intent intent) { 
      //TODO for communication return IBinder implementation 
      return null; 
     } 


     @Override 
     public void onDestroy() { 
      super.onDestroy(); 
      Toast.makeText(getApplicationContext(), "Service Task destroyed", Toast.LENGTH_LONG).show(); 
      Intent myIntent = new Intent(getApplicationContext(), LocationService.class); 

      PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0); 

      AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE); 

      Calendar calendar = Calendar.getInstance(); 

      calendar.setTimeInMillis(System.currentTimeMillis()); 

      calendar.add(Calendar.SECOND, 10); 

      alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

      Toast.makeText(getApplicationContext(), "Start Alarm", Toast.LENGTH_SHORT).show(); 
     } 



     @Override 
     public void onTaskRemoved(Intent rootIntent) { 
      super.onTaskRemoved(rootIntent); 
       Intent myIntent = new Intent(getApplicationContext(), LocationService.class); 

       PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0); 

       AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE); 

       Calendar calendar = Calendar.getInstance(); 

       calendar.setTimeInMillis(System.currentTimeMillis()); 

       calendar.add(Calendar.SECOND, 10); 

       alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

       Toast.makeText(getApplicationContext(), "Start Alarm", Toast.LENGTH_SHORT).show(); 



     } 


     @Override 
     public void onLocationChanged(Location loc) { 
      if (loc == null) // Filtering out null values 
       return ; 

      Double lat = loc.getLatitude(); 
      Double lon = loc.getLongitude(); 
    //  Toast.makeText(getApplicationContext(),"Latitude = "+lat+ "\nLongitude = "+lon, Toast.LENGTH_SHORT).show(); 

     // new UpdateLatitudeLongitude(LocationService.this, lat, lon,).execute(); 

//Calling AsyncTask for upload latitude and longitude 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 






     } 
関連する問題