0

I am sending the location of the user by email on button click. The doInBackground of the AsyncTask is given below.

@Override
protected Boolean doInBackground(String... arg0) {
    // TODO Auto-generated method stub
    boolean status = false;
    try {
    double latitude=0;
    double longitude=0;
    GPSTracker gps=new GPSTracker(MainActivity.this);
    if(gps.canGetLocation()){
        latitude=gps.getLatitude();
        longitude=gps.getLongitude();
    }
    String mapUrl= "https://www.google.co.in/maps/@"+latitude+","+longitude+",17z";
    GMailSender sender = new GMailSender(
        "myemail@gmail.com", "password",
        getApplicationContext());
    status = sender.sendMail(" Subject ", "\nLocation : "+mapUrl,"myemail@gmail.com",
         "anothermail@gmail.com");
    } catch (Exception e) {
    }
    return status;
}

But always returns Location as 0,0

GPSTracker.java

locationManager = (LocationManager) mContext
    .getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
    .isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
    // no network provider is enabled
    } else {
    this.canGetLocation = true;
    if (isNetworkEnabled) {
        locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER,
            MIN_TIME_BW_UPDATES,
            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        Log.d("Network", "Network Enabled");
        if (locationManager != null) {
            location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }
        }
    }
    // if GPS Enabled get lat/long using GPS Services
    if (isGPSEnabled) {
        locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER,                    
                        MIN_TIME_BW_UPDATES,
            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        Log.d("GPS", "GPS Enabled");
        if (locationManager != null) {
            location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }
        }
    }
}

The log shows

05-16 22:01:30.449: W/System.err(12459): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-16 22:01:30.489: W/System.err(12459): at android.os.Handler.<init>(Handler.java:121)
05-16 22:01:30.489: W/System.err(12459): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:198)
05-16 22:01:30.489: W/System.err(12459): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:198)
05-16 22:01:30.499: W/System.err(12459): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:604)
05-16 22:01:30.499: W/System.err(12459): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:471)
05-16 22:01:30.509: W/System.err(12459): at com.package.utils.GPSTracker.getLocation(GPSTracker.java:65)
05-16 22:02:07.309: W/System.err(12459): at com.package.utils.GPSTracker.getLocation(GPSTracker.java:65)
05-16 22:02:07.309: W/System.err(12459): at com.package.utils.GPSTracker.<init>(GPSTracker.java:44)
05-16 22:02:07.309: W/System.err(12459):    at com.webnamaste.ulrasoundindia.MainActivity$SendMailTask.doInBackground(MainActivity.java:255)
05-16 22:02:07.319: W/System.err(12459): at com.package.ulrasoundindia.MainActivity$SendMailTask.doInBackground(MainActivity.java:1)
05-16 22:02:07.319: W/System.err(12459): at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-16 22:02:07.319: W/System.err(12459): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-16 22:02:07.319: W/System.err(12459): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-16 22:02:07.319: W/System.err(12459): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-16 22:02:07.339: W/System.err(12459): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-16 22:02:07.339: W/System.err(12459): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-16 22:02:07.349: W/System.err(12459): at java.lang.Thread.run(Thread.java:856)
Sushin PS
  • 93
  • 1
  • 11

2 Answers2

0

Method doInBackground of AsyncTask is executed on the background thread, it's not a main (UI) thread. Here you can have 3 solutions:

  1. Move getting GPS coordinates procedure to the onPreExecute and pass the result to the doInBackground
  2. Call runOnUiThread at the doInBackground for invoking GPSTracker, this is ugly solution.
  3. Use the combination of Looper.prepare(); and Looper.loop();

Take a look here

Community
  • 1
  • 1
nikis
  • 11,166
  • 2
  • 35
  • 45
  • Moved getting GPS coordinates procedure to the onPreExecute. it now blinks the gps but location is still 0. The google maps of the Mobile shows the correct location. – Sushin PS May 16 '14 at 17:24
  • i am getting NETWORK LOCATION MANAGER = NOT NULL, NETWORK LOCATION = NULL, GPS LOCATION MANAGER = NOT NULL, GPS LOCATION = NULL. while checking the if s in the GPSTracker.java – Sushin PS May 16 '14 at 17:41
  • is the context passed should be MainActivity.this or getApplicationContext() or getBaseContext() – Sushin PS May 16 '14 at 17:49
  • @user3301269 `getApplicationContext()` is better, to avoid memory leaks, but it's not the source of the problem. Are you sure your `GPSTracker` has no bugs? – nikis May 16 '14 at 17:58
  • the class is copy pasted from `androidhive.com`. no bug in that code i think. is it a problem with the device i am testing. should i test on another device or in emulator. but it is `showing exact location in google maps` – Sushin PS May 16 '14 at 18:03
  • @user3301269 does `canGetLocation()` return `true`? if not, that is why you get default values. try to debug your app – nikis May 16 '14 at 18:12
  • i debugged. canGetLocation() returns true. the location got from location manager is null. what do i do? – Sushin PS May 16 '14 at 18:28
  • source not found ILocationManager$Stub$Proxy.getLastKnownLocation(String) line: 889 on debug – Sushin PS May 16 '14 at 18:40
  • @user3301269 sometimes `getLastKnownLocation` can return `null`, because provider is not ready or there is no last known location. you can search for a posts how to handle such cases – nikis May 17 '14 at 08:24
0

You are trying show some data in the gui app, in the doInBackgrund(), the solution is put Looper.preprare() in the first line in doInBackgrund().

protected Boolean doInBackground(String... arg0) {
// TODO Auto-generated method stub
Looper.prepare();//This code
boolean status = false;
try {
double latitude=0;
double longitude=0;
GPSTracker gps=new GPSTracker(MainActivity.this);
if(gps.canGetLocation()){
    latitude=gps.getLatitude();
    longitude=gps.getLongitude();
}
String mapUrl= "https://www.google.co.in/maps/@"+latitude+","+longitude+",17z";
GMailSender sender = new GMailSender(
    "myemail@gmail.com", "password",
    getApplicationContext());
status = sender.sendMail(" Subject ", "\nLocation : "+mapUrl,"myemail@gmail.com",
     "anothermail@gmail.com");
    } catch (Exception e) {
    }
    return status;
}
Chefes
  • 1,892
  • 18
  • 17