The following code shows how to Use location service.
Register permission
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.examples.service" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="1" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ServiceActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService"></service> </application> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> </manifest>
Main layout xml file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/enable" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Tracking" /> <Button android:id="@+id/disable" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Stop Tracking" /> <TextView android:id="@+id/status" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
Main Activity Java code
package com.java2s.myapplication4.app; //from ww w. j a v a 2s . c o m 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; public class MainActivity extends Activity implements View.OnClickListener { Button enableButton, disableButton; TextView statusView; MyService trackerService; Intent serviceIntent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_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, MyService.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 = ((MyService.TrackerBinder)service).getService(); updateStatus(); } public void onServiceDisconnected(ComponentName className) { trackerService = null; } }; }
Service Java code
import java.util.ArrayList; //from www . j av a 2 s . c o m 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; public class MyService 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 { MyService getService() { return MyService.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) { } }