0

Created a Location alarm based app. Everything is working fine on emulator. But the following error occurs in real device.

Scenario:

Turned on GPS. Opened my app in phone. No GPS symbol in status bar. App getting force closed.

To fix this scenario, I am doing the following thing

Open any application like Maps, GPS Essentials. My GPS values is getting fixed. Opening my application after that. App working fine.

Providing the necessary codes below:

in my Oncreate method

fixlocation = orgManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);


@Override
protected void onResume() {
    super.onResume();
    orgManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1000, this);
    orgManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 1000, this);
    mo.enableCompass();
    mo.enableMyLocation();
}

/** Stop the updates when Activity is paused */
@Override
protected void onPause() {
    super.onPause();
    orgManager.removeUpdates(this);
    mo.disableCompass();
    mo.disableMyLocation();
}



@Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        //orgManager.removeUpdates(this);

        if (location != null) {
            currLatitude = location.getLatitude();
            currLongtitude = location.getLongitude();
}}

What do I need to do now?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Satheesh
  • 646
  • 1
  • 10
  • 33

1 Answers1

0
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

currLat = location.getLatitude();
currentLang = location.getLongitude();

Try this to get current location.

onLocationChanged will call only when location of your device changes but

first time when your application starts you can use this code.

MAC
  • 15,799
  • 8
  • 54
  • 95