Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:58:47+00:00 2026-06-03T13:58:47+00:00

I have implemented a simple GPS program that fetches the co-ordinates and displays them

  • 0

I have implemented a simple GPS program that fetches the co-ordinates and displays them on the screen. When I tried to improve on this design and implement an async task, the GPS doesn’t seem to work for some reason or another. Is there some issue with regards to using async task with GPS? Here is my code:

private class DownloadTask extends AsyncTask<String, Void, Object> {
        protected Object doInBackground(String... args) {
            Log.i("MyApp", "Background thread starting");

            LocationManager mLocationManager;
            Gpslistener Gpslistener;

            Gpslistener = new Gpslistener();

            try{

               mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

               mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,Gpslistener);

            }catch (Exception e) {

                test_gps = true;
           }


        return null;
    }

    protected void onPostExecute(Object result) {

        if(test_gps == true){
            if (ParkingActivity.this.progressDialog != null) {
                ParkingActivity.this.progressDialog.dismiss();
            }               
            AlertMessage("GPS Error", "Unable to get location");
        }



public class Gpslistener implements LocationListener
{
        @Override
        public void onLocationChanged(Location loc)
        {
            loc.getLatitude();
            loc.getLongitude();
        }

        @Override
        public void onProviderDisabled(String provider)
        {       
        }

        @Override
        public void onProviderEnabled(String provider)
        {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
        }
    }

Each time I run it, it always catches an exception. The GPS permissions are set in the manifest and I’m always using a different application to ensure that a GPS connection is online so I eliminated those as potential errors. Honestly I can’t think of anything else, without the async task it works perfectly! Any help is much appreciated, thanks!

Edited
My exception logcat:

05-09 22:56:20.199: E/EXCEPTION:(8874): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-09 22:56:20.199: E/EXCEPTION:(8874): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.os.Handler.<init>(Handler.java:121)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:173)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:173)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.location.LocationManager._requestLocationUpdates(LocationManager.java:579)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at stefan.testservice.ParkingActivity$DownloadTask.doInBackground(ParkingActivity.java:163)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at stefan.testservice.ParkingActivity$DownloadTask.doInBackground(ParkingActivity.java:1)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at java.lang.Thread.run(Thread.java:1019)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:58:48+00:00Added an answer on June 3, 2026 at 1:58 pm

    You can’t do that. The thread dies as soon as doInBackground() returns. The GPS listener is a Handler which requires a Looper thread to operate. At the beginning of doInBackground() you need to call Looper.prepare() then at the end call Looper.loop(). It will loop forever until you call quit() on the thread’s Looper object.

    AsyncTask is really designed for one-shot temporary threads though, so I’d use a normal thread for this. Although, there’s no reason you couldn’t I suppose. You just have to make 100% sure that you end the loop or it will run forever even after the user closes the app.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a weired problem, I have implemented simple quick sort as follows.. static
I have implemented a simple server-client script like this: Server: class Server(Protocol): def connectionMade(self):
I have implemented a simple appmod that handle WebSockets and echo back the messages.
I have implemented a simple parallel merge sort algorithm in Java. This cuts the
I have implemented a simple search function that highlights a row in a DataGrid
I have Implemented a simple program which simulates $ls -l | wc -c command
I have this simple service that broadcasts the current location of the user. I
I have a service that runs this thread to get GPS coordinates LocationManager lm
I have implemented a simple D3.js line chart that can be zoomed and panned.
I have implemented a simple chat program in Java . However when I run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.