I am trying to enable a location listener to provide information if a phone has moved more than a certain distance when a checkbox is checked. For a test, I just want to increment a counter up by 1 if I have moved more than 1 meters in 10 seconds.
I am able to get the location listener to work and the checkbox onclick listener to work. However, I am having issues trying to remove the locationlistener when the box is not checked. In addition, I am getting multiple increments when I recheck the box. Below is my code. I call the testgps() method from the OnCreate() method of my Class.
If you have any suggestions, they would be appreciated.
Thx
private void testgps() {
final CheckBox gps_enb = (CheckBox) findViewById(R.id.checkBox_gps);
gps_enb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LocationManager loc = (LocationManager) getSystemService(LOCATION_SERVICE);
if (((CheckBox) v).isChecked()) {
long minTime = 10000;
float minDistance = 1;
loc.requestLocationUpdates( LocationManager.GPS_PROVIDER, minTime,minDistance, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
TextView count = (TextView) findViewById(R.id.textView_GPS_Listener_debug);
int buffer = new Integer(count.getText().toString()).intValue() + 1;
StringBuilder count_up = new StringBuilder();
count_up.append(buffer);
count.setText(count_up);
}
});
Toast.makeText(QuizSettingsActivity.this, "True",
Toast.LENGTH_SHORT);
Toast.makeText(QuizSettingsActivity.this, "Selected", Toast.LENGTH_SHORT).show();
} else {
loc = null;
Toast.makeText(QuizSettingsActivity.this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
});
Hey thanks for the help. This is what i ended up with. I dug a bit more into Non-Access Modifiers.
private void testgps() {
final CheckBox gps_enb = (CheckBox) findViewById(R.id.checkBox_gps);//Checkbox to Enable GPS
final LocationManager loc = (LocationManager)getSystemService(LOCATION_SERVICE);//Declare location mgr
//Location Listner
final LocationListener loc_listener= new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
TextView count = (TextView) findViewById(R.id.textView_GPS_Listener_debug);
int buffer = new Integer(count.getText().toString()).intValue() + 1;
StringBuilder count_up = new StringBuilder();
count_up.append(buffer);
count.setText(count_up);
}
};
gps_enb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = gps_enb.isChecked();
if (checked==true) {
long minTime = 10000;
float minDistance = 1;
loc.requestLocationUpdates( LocationManager.GPS_PROVIDER, minTime,minDistance, loc_listener);//Provide Location Updates
Toast.makeText(QuizSettingsActivity.this, "Selected", Toast.LENGTH_SHORT).show();
};
if (checked!=true){
loc.removeUpdates(loc_listener);
Toast.makeText(QuizSettingsActivity.this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
});
you can add a method that removes the listener as follows:
private void removeMyLocationListener(LocationManager loc){
loc.removeUpdates(your listener);
}
and you'll call it inside the OnCheckChangeListener
if your check box is not checked as follows:
chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(!isChecked){removeMyLocationListener(LocationManager loc);}
}
});