1

Is it possible, to use getSystemService in service? My service class:

private LocationManager locationManager;
private LocationListener locationListener;

IBinder mBinder = new LocalBinder();      

public interface interfaceLocationService {     
      public Location currentBestLocation = null;
      public void startListenLocation();
      public void stopListenLocation();
      public Location getUserLocation();
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public class LocalBinder extends Binder implements interfaceLocationService{

    public void StopListenLocation(){
        locationManager.removeUpdates(locationListener);
        locationListener = null;
        stopSelf();
    }

    public void startListenLocation()
    { 
        locationListener = new LocationListener() {

            public void onStatusChanged(String provider, int status, Bundle extras) {    
                switch (status) {

                // Provider out of service
                case LocationProvider.OUT_OF_SERVICE:
                    Log.v(TAG, "Provider status changed: Out of Service");
                    break;

                // Provider temporarily unavailable
                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    Log.v(TAG, "Provider status changed: Temporarily Unavailable");
                    break;

                // Provider available again
                case LocationProvider.AVAILABLE:
                    Log.v(TAG, "Provider status changed: Available");
                    break;
                }
            }       

            public void onProviderEnabled(String provider) {    
                Log.v(TAG, "Provider enabled");
            }

            public void onProviderDisabled(String provider) {    
                Log.v(TAG, "Provider disabled");

                // Settings brought up to connect
                Intent intent = new Intent(
                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }

            public void onLocationChanged(Location location) {                  
                Log.v(TAG, "Location Changed");
                if (isBetterLocation(location, currentBestLocation)) {
                    currentBestLocation.set(location);
                }
            }
        };
        //if I have only one requestLocationUpdates situation is the same
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
                    400, 1, locationListener);
        }

    public Location getUserLocation(){
        return currentBestLocation;
    }

    @Override
    public void stopListenLocation() {
        locationManager.removeUpdates(locationListener);
        locationListener = null;
        stopSelf();
    }
}

@Override
public void onCreate() {
    // Start thread  
    locationManager = (LocationManager)SendService.this.getSystemService(Context.LOCATION_SERVICE);
    super.onCreate();
    Log.d(TAG, "SendService created");
}

@Override
public void onDestroy() {
    // Just destroy thread
    super.onDestroy();
    Log.d(TAG, "SendService destroyed");
}

And in activity I got code starting this service:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = new Intent(this, SendService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className,
            IBinder service) {
        mService = (interfaceLocationService) service; 
    }

    public void onServiceDisconnected(ComponentName arg0) {
        Log.d("LOG","Service disconnected");
        mService.stopListenLocation();
    }
};

public void startClicked(View v) {
    mService.startListenLocation();   
}

This code gives an errors in LogCat:

11-12 17:01:02.171: E/AndroidRuntime(2719): FATAL EXCEPTION: main
11-12 17:01:02.171: E/AndroidRuntime(2719): android.util.AndroidRuntimeException:      Calling startActivity() from outside of an Activity  context requires the      FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.app.ContextImpl.startActivity(ContextImpl.java:657)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at com.example.gps.data.SendService$LocalBinder$1.onProviderDisabled(SendService.java:78)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:204)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.os.Looper.loop(Looper.java:143)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.app.ActivityThread.main(ActivityThread.java:4717)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at java.lang.reflect.Method.invoke(Method.java:521)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at dalvik.system.NativeStart.main(Native Method)

Simply: Could I use LocationManager to listen to location in service while in background? If yes, then where is my mistake?

1
  • please share the full stack trace from logcat... Nov 12, 2012 at 14:10

1 Answer 1

2

Yes of course you can use the LocationManager in a service. for instance I am doing this in the onCreate method of the background service :

public void onCreate() {
      mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

      Criteria criteria = new Criteria();
      criteria.setAccuracy(Criteria.ACCURACY_COARSE);
      criteria.setAltitudeRequired(false);
      criteria.setBearingRequired(false);
      criteria.setCostAllowed(true);

      String provider = mLocationManager.getBestProvider(criteria, true);
      mLocationManager.requestLocationUpdates(provider, LOCATION_UPDATE_INTERVAL_MILLIS, LOCATION_UPDATE_INTERVAL_METERS, this);

    }
2
  • And should it work even if service is started from activity? I'll put full stack trace when at home.
    – Karol Gil
    Nov 12, 2012 at 14:31
  • Yes i am starting the service from the splash activity Nov 12, 2012 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.