1. Download our Official Android App: Forums for Android!

Apps how to get notified from updates in Service via ServiceConnection

Discussion in 'Android Development' started by tugceg, Aug 15, 2011.

  1. tugceg

    tugceg Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Aug 15, 2011

    Aug 15, 2011
    1
    0
    5
    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 (Text):
    1. public class TrackerService extends Service implements LocationListener {
    2.  
    3. private static final String LOGTAG = "TrackerService";
    4.  
    5. private LocationManager manager;
    6. private ArrayList<Location> storedLocations;
    7.  
    8. private boolean isTracking = false;
    9.  
    10. /* Service Setup Methods */
    11. @Override
    12. public void onCreate() {
    13.     manager = (LocationManager)getSystemService(LOCATION_SERVICE);
    14.     storedLocations = new ArrayList<Location>();
    15.     Log.i(LOGTAG, "Tracking Service Running...");
    16. }
    17.  
    18. public void startTracking() {
    19.     if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    20.         return;
    21.     }
    22.     Toast.makeText(this, "Starting Tracker", Toast.LENGTH_SHORT).show();
    23.     manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this);
    24.  
    25.     isTracking = true;
    26. }
    27.  
    28. public void stopTracking() {
    29.     Toast.makeText(this, "Stopping Tracker", Toast.LENGTH_SHORT).show();
    30.     manager.removeUpdates(this);
    31.     isTracking = false;
    32. }
    33.  
    34. public boolean isTracking() {
    35.     return isTracking;
    36. }
    37.  
    38. @Override
    39. public void onDestroy() {
    40.     manager.removeUpdates(this);
    41.     Log.i(LOGTAG, "Tracking Service Stopped...");
    42. }
    43.  
    44. /* Service Access Methods */
    45. public class TrackerBinder extends Binder {
    46.     TrackerService getService() {
    47.         return TrackerService.this;
    48.     }
    49. }
    50.  
    51. private final IBinder binder = new TrackerBinder();
    52.  
    53. @Override
    54. public IBinder onBind(Intent intent) {
    55.     return binder;
    56. }
    57.  
    58. public int getLocationsCount() {
    59.     return storedLocations.size();
    60. }
    61.  
    62. public ArrayList<Location> getLocations() {
    63.     return storedLocations;
    64. }
    65.  
    66. /* LocationListener Methods */
    67. @Override
    68. public void onLocationChanged(Location location) {
    69.     Log.i("TrackerService", "Adding new location");
    70.     storedLocations.add(location);
    71. }
    72.  
    73. @Override
    74. public void onProviderDisabled(String provider) { }
    75.  
    76. @Override
    77. public void onProviderEnabled(String provider) { }
    78.  
    79. @Override
    80. public void onStatusChanged(String provider, int status, Bundle extras) { }
    81. }
    82.  
    83. public class ServiceActivity extends Activity implements View.OnClickListener {
    84.  
    85. Button enableButton, disableButton;
    86. TextView statusView;
    87.  
    88. TrackerService trackerService;
    89. Intent serviceIntent;
    90.  
    91. @Override
    92. public void onCreate(Bundle savedInstanceState) {
    93.     super.onCreate(savedInstanceState);
    94.     setContentView(R.layout.main);
    95.  
    96.     enableButton = (Button)findViewById(R.id.enable);
    97.     enableButton.setOnClickListener(this);
    98.     disableButton = (Button)findViewById(R.id.disable);
    99.     disableButton.setOnClickListener(this);
    100.     statusView = (TextView)findViewById(R.id.status);
    101.  
    102.     serviceIntent = new Intent(this, TrackerService.class);
    103. }
    104.  
    105. @Override
    106. public void onResume() {
    107.     super.onResume();
    108.     //Starting the service makes it stick, regardless of bindings
    109.     startService(serviceIntent);
    110.     //Bind to the service
    111.     bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
    112. }
    113.  
    114. @Override
    115. public void onPause() {
    116.     super.onPause();
    117.     if(!trackerService.isTracking()) {
    118.         //Stopping the service let's it die once unbound
    119.         stopService(serviceIntent);
    120.     }
    121.     //Unbind from the service
    122.     unbindService(serviceConnection);
    123. }
    124.  
    125. @Override
    126. public void onClick(View v) {
    127.     switch(v.getId()) {
    128.     case R.id.enable:
    129.         trackerService.startTracking();
    130.         break;
    131.     case R.id.disable:
    132.         trackerService.stopTracking();
    133.         break;
    134.     default:
    135.         break;
    136.     }
    137.     updateStatus();
    138. }
    139.  
    140. private void updateStatus() {
    141.     if(trackerService.isTracking()) {
    142.         statusView.setText(String.format("Tracking enabled.  %d locations logged.",trackerService.getLocationsCount()));
    143.     } else {
    144.         statusView.setText("Tracking not currently enabled.");
    145.     }
    146. }
    147.  
    148. private ServiceConnection serviceConnection = new ServiceConnection() {
    149.     public void onServiceConnected(ComponentName className, IBinder service) {
    150.         trackerService = ((TrackerService.TrackerBinder)service).getService();
    151.         updateStatus();
    152.     }
    153.  
    154.     public void onServiceDisconnected(ComponentName className) {
    155.         trackerService = null;
    156.     }
    157. };
    158. }
     

    Advertisement

Share This Page

Loading...