I have this simple service that transfers the current location of the user. I want to use the binding mechanism only to control the life cycle of the service, but the service just does not start.
What have I done wrong?
public class GPSActivity extends ListActivity { ... protected void onResume() { super.onResume(); Log.i("Service", "Service bound"); Intent intent = new Intent(this, LocationService.class); bindService(intent, service_connection , Context.BIND_AUTO_CREATE); } protected void onPause() { if (dataUpdateReceiver!=null) unregisterReceiver(dataUpdateReceiver); unbindService(service_connection); super.onPause(); } class LocationServiceConnection implements ServiceConnection{ public void onServiceConnected(ComponentName name, IBinder service) { Log.i("Service", "Service Connected"); } public void onServiceDisconnected(ComponentName name) { } } }
LocalBinder.java
public class LocalBinder<S> extends Binder { private String TAG = "LocalBinder"; private WeakReference<S> mService; public LocalBinder(S service){ mService = new WeakReference<S>(service); } public S getService() { return mService.get(); } }
LocationService.java
public class LocationService extends Service { public void onCreate() { initLocationListener(); Log.i("Location Service","onCreate()"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("Location Service", "Received start id " + startId + ": " + intent); return START_NOT_STICKY; } private final IBinder mBinder = new LocalBinder<LocationService>(this); @Override public IBinder onBind(Intent intent) { return mBinder; } }
AndroidManifest.xml
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > ... <service android:name=".LocationService"> </service> </application>
EDIT: Fixed thanks to NickT's answer.
There was no intent filter or valid name in the manifest entry
<service android:enabled="true" android:name="com.android.gps.services.LocationService"> <intent-filter> <action android:name="com.android.gps.services.LocationService" /> </intent-filter> </service>
And the intention that I used to bind was similar to the ones you should use at startup. correct:
Intent intent = new Intent("com.android.gps.services.LocationService");
bughi
source share