• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps how to get notified from updates in Service via ServiceConnection

tugceg

Lurker
Aug 15, 2011
1
0
I am all confused about Service, Activity, Intent concepts. What I am trying to do is TrackerMapService to work in background every 5 sec check location, and if it is changed TextView of the Activity will update number of locations tracked.

Code:
public class TrackerService extends Service implements LocationListener {

private static final String LOGTAG = "TrackerService";

private LocationManager manager;
private ArrayList<Location> storedLocations;

private boolean isTracking = false;

/* Service Setup Methods */
@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...");
}

/* Service Access Methods */
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;
}

/* LocationListener Methods */
@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 ServiceActivity 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();
    //Starting the service makes it stick, regardless of bindings
    startService(serviceIntent);
    //Bind to the service
    bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}

@Override
public void onPause() {
    super.onPause();
    if(!trackerService.isTracking()) {
        //Stopping the service let's it die once unbound
        stopService(serviceIntent);
    }
    //Unbind from the service
    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;
    }
};
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones