Mega Code Archive

 
Categories / Android / Network
 

Tracker Service

package app.test; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.widget.Button; import android.widget.TextView; import android.view.View; import java.util.ArrayList; import android.app.Service; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.widget.Toast;  class TrackerService extends Service implements LocationListener {     private static final String LOGTAG = "TrackerService";          private LocationManager manager;     private ArrayList<Location> storedLocations;          private boolean isTracking = false;          @Override     public void onCreate() {         manager = (LocationManager)getSystemService(LOCATION_SERVICE);         storedLocations = new ArrayList<Location>();         Log.i(LOGTAG, "Tracking Service Running...");     }          public void startTracking() {         if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {             return;         }         Toast.makeText(this, "Starting Tracker", Toast.LENGTH_SHORT).show();         manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this);                  isTracking = true;     }     public void stopTracking() {         Toast.makeText(this, "Stopping Tracker", Toast.LENGTH_SHORT).show();         manager.removeUpdates(this);         isTracking = false;     }          public boolean isTracking() {         return isTracking;     }          @Override     public void onDestroy() {         manager.removeUpdates(this);         Log.i(LOGTAG, "Tracking Service Stopped...");     }     public class TrackerBinder extends Binder {         TrackerService getService() {             return TrackerService.this;         }     }          private final IBinder binder = new TrackerBinder();          @Override     public IBinder onBind(Intent intent) {         return binder;     }          public int getLocationsCount() {         return storedLocations.size();     }          public ArrayList<Location> getLocations() {         return storedLocations;     }     @Override     public void onLocationChanged(Location location) {         Log.i("TrackerService", "Adding new location");         storedLocations.add(location);     }     @Override     public void onProviderDisabled(String provider) { }     @Override     public void onProviderEnabled(String provider) { }     @Override     public void onStatusChanged(String provider, int status, Bundle extras) { } } public class Test extends Activity implements View.OnClickListener {          Button enableButton, disableButton;     TextView statusView;          TrackerService trackerService;     Intent serviceIntent;          @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);                  enableButton = (Button)findViewById(R.id.enable);         enableButton.setOnClickListener(this);         disableButton = (Button)findViewById(R.id.disable);         disableButton.setOnClickListener(this);         statusView = (TextView)findViewById(R.id.status);                  serviceIntent = new Intent(this, TrackerService.class);     }          @Override     public void onResume() {         super.onResume();         startService(serviceIntent);         bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);     }     @Override     public void onPause() {         super.onPause();         if(!trackerService.isTracking()) {             stopService(serviceIntent);         }         unbindService(serviceConnection);     }          @Override     public void onClick(View v) {         switch(v.getId()) {         case R.id.enable:             trackerService.startTracking();             break;         case R.id.disable:             trackerService.stopTracking();             break;         default:             break;         }         updateStatus();     }          private void updateStatus() {         if(trackerService.isTracking()) {             statusView.setText(String.format("Tracking enabled.  %d locations logged.",trackerService.getLocationsCount()));         } else {             statusView.setText("Tracking not currently enabled.");         }     }          private ServiceConnection serviceConnection = new ServiceConnection() {         public void onServiceConnected(ComponentName className, IBinder service) {             trackerService = ((TrackerService.TrackerBinder)service).getService();             updateStatus();         }         public void onServiceDisconnected(ComponentName className) {             trackerService = null;         }     }; }