Reputation: 1199
I've read a lot of posts on stackoverflow about that, but I didn't find the answer that I need. Finding my location is very simple using the gps or using the network provider, but there is a problem. My app at first check if the gps is active and if not, I send the user to the gps option page. After that the user can go back on the first activity. I use the current location in this activity and in a second activity. I put the listener only in the first activity and in the function "onLocationUpdate()" I save my latitude and longitude in my global application variables. If I start the app then I set enabled the gps and then I open my second activity, the lat and lon are equals 0.0, but if I start the activity, then I set enabled the gps, then I wait 30 seconds, it works. Does it exist one method to eliminate this delay? Using the Network_Provider is faster, but it isn't precise like the gps!
The pieces of code are those:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean gpsStatus = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsStatus) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
alertDialogBuilder.setTitle("The GPS is off!");
alertDialogBuilder
.setMessage("Turn on the GPS?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Intent intent1 = new Intent();
intent1.setClassName(
"com.android.settings",
"com.android.settings.SecuritySettings");
startActivity(intent1);
dialog.cancel();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, locationListener);
This part is for check if the gps is enabled. Does it exist a method to enabled GPS without going in the setting page?
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) { // update
if (location != null) {
msg_r.setLatitude(location.getLatitude());
msg_r.setLongitude(location.getLongitude());
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
This is my listener.
Upvotes: 0
Views: 2746
Reputation: 1133
GPS needs time to get your current location. There's no way around it. You could use the method getLastKnownLocation()
if you want it to be faster, or like you said you could use a network provider. However the last known location may not be accurate at all.
The GPS isn't ready until the first time it calls onLocationUpdate()
. So you could make the user stay on the first activity until this event has occurred, then allow the user to go to the second activity. Or have the second activity get location updates. There are different things you could do.
As for enabling GPS without going to the settings page, you cannot. I cannot find the question, but CommonsWare just answered this question a few hours ago stating that this cannot happen because it is a privacy issue.
Upvotes: 1
Reputation: 17960
You should give Reto Meier's articles on building location based apps a read:
Upvotes: 2