How can you ensure a good GPS signal is found before allowing a button click which starts logging GPS locations?
Asked
Active
Viewed 822 times
3 Answers
0
Well, if you use the LocationManager
-class and act upon the onLocationChanged()
, it won't actually trigger until it's gotten a position.
Edit: Here's something you can try out. Edit#2: D'oh, I actually misread the question. I'll leave my response though.
.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/mybutton1"
android:visibility="INVISIBLE"
android:layout_width="200dp"
android:layout_height="100dp"
android:text="MyButton1"
/>
</LinearLayout>
.java:
//Get the LocationManager & Button
final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final Button button = (Button) findViewById(R.id.mybutton1);
//Add the listener
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
//Remove the listener and make the button visible
locManager.removeUpdates(locationListener);
button.setVisibility(1);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Let me know if this works out for you!

karllindmark
- 6,031
- 1
- 26
- 41
-
Is it possible to set the button visibility from a service? The LocationListener is part of a service in the application. Thanks. – danny Aug 12 '11 at 14:36
-
I'm not too sure about that, but would you be able to pass it to your application via a variable instead, and check upon starting the activity? That would be a static solution though. – karllindmark Aug 12 '11 at 14:40
0
To check for a GPS fix, you'll have to register a gpsstatus.listener
and look at the GPS status and the timing in between updates.
Check out this link for how: How can I check the current status of the GPS receiver?
-1
You can use this code inside your button click to check the status of GPS.
LocationListener ll = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, ll);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.i("About GPS", "GPS is Enabled in your devide");
AlertDialog.Builder GPSON =new Builder(MyCardio.this);
GPSON.setCancelable(false);
GPSON.setMessage("GPS IS ON");
GPSON.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
GPSON.show();
} else {
AlertDialog.Builder GPSOFF = new Builder(MyCardio.this);
GPSOFF.setCancelable(false);
GPSOFF.setTitle("Connection Error");
GPSOFF.setMessage(" Please Enable Your GPS !");
GPSOFF.setPositiveButton("Ok",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
// startActivity(new Intent(LoginActivity.this,MainMenuActivity.class));
}
});
GPSOFF.show();
}

Randroid
- 3,688
- 5
- 30
- 55
-
I don't think this checks to be sure there is a GPS fix -- this only check to see that the GPS receiver is enabled. GPS can be enabled without having a proper fix. Plus, getLastKnownLocation does not ensure a location from the "current gps session". This code would return true if you had your gps receiver on 1000m underground. – Ian Aug 12 '11 at 14:30