0

i have a problem. When i test this. and i ask for the showCurrentLocation function it always returns null. It works in the emulator when i send the location after. But i need this to work on the phone, and there you can't send the location like in de DDNS window.

Here's my code

public class LbsGeocodingActivity extends Activity {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1; // in Milliseconds

protected LocationManager locationManager;

protected Button retrieveLocationButton;
protected Button stopLocationButton;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
    );

    retrieveLocationButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showCurrentLocation();
        }
    });  
    /*stopLocationButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //locationManager.removeUpdates(MyLocationListener)  ;          
        }
    }); */ 

}    

public String getMyPhoneNumber(){
    TelephonyManager mTelephonyMgr;
    mTelephonyMgr = (TelephonyManager)
    getSystemService(Context.TELEPHONY_SERVICE); 
    return mTelephonyMgr.getLine1Number();
}

protected void showCurrentLocation() {

    Criteria crit = new Criteria();
    crit.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(crit, true);
    Location loc = locationManager.getLastKnownLocation(provider);


    if (loc != null) {
        String longi = "" + loc.getLongitude();
        String lat = "" + loc.getLatitude();
        String num = getMyPhoneNumber();
        String message = String.format(
                "Current Location \n  Longitude: %1$s \n Latitude: %2$s \n %3$s ",
                longi,
                lat,
                num         );
        Toast.makeText(LbsGeocodingActivity.this, message,
                Toast.LENGTH_LONG).show();
    }
    if (loc == null)Toast.makeText(LbsGeocodingActivity.this, "Null ",
            Toast.LENGTH_LONG).show();
}   

private class MyLocationListener implements LocationListener {



    public void onLocationChanged(Location loc) {
        String longi = "" + loc.getLongitude();
        String lat = "" + loc.getLatitude();
        String num = getMyPhoneNumber();
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s \n %3$s ",
                longi,
                lat,
                num                 );
        Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String s, int i, Bundle b) {
        Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String s) {
        Toast.makeText(LbsGeocodingActivity.this,
                "Provider disabled by the user. GPS turned off",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        Toast.makeText(LbsGeocodingActivity.this,
                "Provider enabled by the user. GPS turned on",
                Toast.LENGTH_LONG).show();
    }

}

}

My permissions are: FINE_LOCATION COURSE_LOCATION.

I really just want it that it tracks the users location, even on the background..

j0k
  • 22,600
  • 28
  • 79
  • 90
David Raijmakers
  • 1,369
  • 1
  • 16
  • 40

2 Answers2

0

You can sent mock locations also to your Android device see Android mock location on device?

Then besides that I had this problem too it seems it maybe never had a location before on your device try to open google maps and make sure you get located and then try again. Also I suspected something like first time you use the application you don't have access to a last know location yet because you never used it so you first need to get located and next time you startup the application it will work. If you want a quick location try to get located by wifi or cell towers

And make sure the permissions are set!

Community
  • 1
  • 1
QVDev
  • 1,093
  • 10
  • 17
  • I tried to locate it in google maps. But still the showCurrentLocation gives null.. I really have no idea what i'm doing wrong. I just need it that the app gives the current location, even when it's in the background – David Raijmakers Jul 11 '12 at 09:02
  • Do you get a location in google maps? if not are you sure you have the settings right on you're phone? The icon above should be showed until you have a location in google maps, so make sure it works there, then see if you get same behavior in your own application. Like I mention above it seems last know location is null because its the first time you are using the application so it is not known yet, you should first try to get location updates normal way then. – QVDev Jul 11 '12 at 09:10
  • Yes, the location is perfectly fine. I do this with the gps on. And the WIFI on? is that a problem? I tested it on a Samsung Galaxy SII – David Raijmakers Jul 11 '12 at 09:12
  • So the only problem it occurs when you are using you're own developed location aware application? Try instead of getting last known get the precise location when the last known is null. This is normal behavior – QVDev Jul 11 '12 at 09:13
  • Here a great explanation from google itself http://developer.android.com/guide/topics/location/strategies.html – QVDev Jul 11 '12 at 09:23
  • If you have the exact code it should work make sure the permissions are set right and also in your phone. Then restart your phone, and maybe try another phone with this APK to rule out that it is you're device that is the bottleneck – QVDev Jul 11 '12 at 09:32
  • Thanks, i will try this for a second.. but i still think it's crazy that it first needs to give null to the showCurrentLocation function.. There gotta be a bug in there – David Raijmakers Jul 11 '12 at 09:35
0

did you checked the GPS is switched on in your phone, after switching on GPS it will take some time to get the location updates.

Vishwanath.M
  • 6,235
  • 11
  • 42
  • 56