使用GPS获取MyLocation - 如何实现'if else'语句?

时间:2012-05-18 13:54:04

标签: java android windows-mobile-gps

我是android的新手,并且以逆向工程学习java。

使用下面的代码,我有一些问题需要在定制时解决,以适应我的想法。

@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)

{

问题是我的(android)主屏幕以一个显示两个选项(checboxes)的复选页面开始;

GPS开启(启用) GPS关闭(已禁用)

现在,问题是,我不知道如何编写'if else'语句/方法,这可能有助于将'On'场景指向获取我的位置的下一阶段,并指导'Off'场景回到开头(主屏幕)。

我在代码中的位置/方式声明/插入复选框代码?

欢迎任何协助。

由于

1 个答案:

答案 0 :(得分:0)

基本复选框检查:

 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();
           }
      }
  });