Passing context for call to “getSystemService”

My application crashes when accessing a location service within a method which is called within a Thread. I pass the context from within a service in which the thread is running but it doesn't work.

This is the call to the LocationService within the thread which is created inside a service.

class MyService extends Service {

  public int onStartCommand(Intent intent, int flags, int startId) {

  Thread thread = new Thread(){
    public void run() {

        geofix = new GeoFixer(getApplicationContext());
      }catch (....){}....

The constructor of GeoFixer takes the context and saves it in Context context.

Now inside the method startLocationObserver() I call getSystemService with this context which crashes the application.

public class GeoFixer {
  private Context context;

  GeoFixer(Context context){
    this.context = context; 

  public void startLocationObserver(){
     LocationManager locationManager = (LocationManager)     
  // This is where it crashes
  ... }

What am I doing wrong?

EDIT: Here's the LogCat now.

ERROR/AndroidRuntime(8824): FATAL EXCEPTION: Thread-10
ERROR/AndroidRuntime(8824): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
ERROR/AndroidRuntime(8824): at android.os.Handler.<init>(
ERROR/AndroidRuntime(8824): at android.location.LocationManager$ListenerTransport$1.<init>(
ERROR/AndroidRuntime(8824): at android.location.LocationManager$ListenerTransport.<init>(
ERROR/AndroidRuntime(8824): at android.location.LocationManager._requestLocationUpdates(
ERROR/AndroidRuntime(8824): at android.location.LocationManager.requestLocationUpdates(
ERROR/AndroidRuntime(8824): at de.theapplication.GeoFixer.getNetworkLocation(
ERROR/AndroidRuntime(8824): at de.theapplication.GeoFixer.startLocationObserver(
ERROR/AndroidRuntime(8824): at de.theapplication.Fadenzieher$


Looking at the stack trace, looks like your startLocationObserver() method invoked the LocationManager's requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) method (the one without a Looper argument).

See if you can call the requestLocationUpdates() version with the Looper argument instead, passing Looper.getMainLooper().

Alternatively, see this blog post where the solution is to call Looper.myLooper().prepare().

From a quick search on Google, it seems that worker thread do not have a Looper associated with them, so your Runnable might be trying to post a message on a thread that cannot process them. Try posting the same Runnable to your UI thread and see if the error still occurs.


Android Can't create handler inside thread that has not called Looper.prepare()

Get a reference to the LocationService in onStartCommand() (main/UI thread), and pass that to your thread.

And why don't you want to use an IntentService? It takes care of running your code on a separate thread and shuts itself down when done.

Need Your Help

Respond to events in other JPanels

java swing event-handling

Ok so lets say I have 3 Jpanels - 1 parent and 2 Children

Attempted to call method “getDoctrine” on class “Symfony\Component\HttpFoundation\Response”

php symfony2 doctrine2

I have been trying to add multiple cars to a user but I always get this error,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.