First of all, Service
extends ContextWrapper
and is therefore a Context
. If you need a reference to a Context you can simply reference your Service. You cannot cast a Service's base Context to an Activity
.
If you want to work on the UI Thread from a Service, have a look at creating a Handler
with Looper.getMainLooper()
.
...
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
});
...
The Android documentation offers good information on communicating to the UI thread. Have a look here:
http://developer.android.com/training/multiple-threads/communicate-ui.html