I am testing my application in Android Emulator and I am only able to set a GPS position when the application is already running through the onLocationChanged method. I can’t set it before I start the application. This is the code in my onCreate method:
Edit: Think you guys missunderstood me. I am able to set a GPS position with geo fix when application is running. When I try to set it before I launch the activity I get Error android.view.windowmanager$badtokenexception.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Criteria crit = new Criteria();
providers = lm.getBestProvider(crit, false);
loc = lm.getLastKnownLocation(providers);
if (loc != null) {
title.setText(stop_name);
lview.setAdapter(lviewAdapter);
} else {
Toast t = Toast.makeText(this, "Failed to get GPS location", 5000);
t.setGravity(Gravity.BOTTOM, 0, 0);
t.show();
}
to set the gps location in emulator you have to follow below steps and open your app before setting up below steps ,then this will go to last configuration .