Get MyLocation using GPS - how to implement 'if else' statement?

557 Views Asked by At

I am new to android, and reverse-engineering my way into learning java.

Having got as far using the code below, i have a few problems needing to be resolved while customising it to suit my inentions.

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

/* Use the LocationManager class to obtain GPS locations */

LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

}

/* Class My Location Listener */

public class MyLocationListener implements LocationListener

{

@Override

public void onLocationChanged(Location loc)

{

loc.getLatitude();

loc.getLongitude();

String Text = “My current location is: “ +

“Latitud = “ + loc.getLatitude() +

“Longitud = “ + loc.getLongitude();

Toast.makeText( getApplicationContext(),

Text,

Toast.LENGTH_SHORT).show();

}

@Override

public void onProviderDisabled(String provider)

{

Toast.makeText( getApplicationContext(),

“Gps Disabled”,

Toast.LENGTH_SHORT ).show();

}

@Override

public void onProviderEnabled(String provider)

{

Toast.makeText( getApplicationContext(),

“Gps Enabled”,

Toast.LENGTH_SHORT).show();

}

@Override

public void onStatusChanged(String provider, int status, Bundle extras)

{

The problem is my (android) home screen starts with a checkboxed page showing two choices (checboxes);

GPS On (Enabled) GPS Off (Disabled)

Now, question is, i have no idea how to write the 'if else' statement/method which could which could help direct the 'On' scenario to the next stage of getting my location, and for directing the 'Off' scenario back to beginning (Home screen).

Where/how in the code do i declare/insert the checkbox code?

Any assistance welcome please.

Thanks

1

There are 1 best solutions below

1
Rick On

Basic checkbox check:

 final CheckBox cbGPS = (CheckBox) findViewById(R.id.checkBox1);
 cbGPS.setOnCheckedChangeListener(new OnCheckedChangeListener() {

      public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            // TODO Auto-generated method stub
            if (buttonView.isChecked()) {
                  // do this if checked

                  StartGPSMethod();
           } else {
                  // if not checked do this
                  // do nothing  or

                    EndGPSMethod();
           }
      }
  });