Android:位置监听器

时间:2012-01-19 19:00:23

标签: android locationlistener

我正在尝试启用位置监听器,以便在选中复选框时,如果手机移动的距离超过一定距离,则会提供信息。对于测试,如果我在10秒内移动超过1米,我只想将计数器增加1。

我能够让位置监听器工作,并且复选框onclick监听器可以正常工作。但是,在未选中该框时,我在尝试删除locationlistener时遇到问题。另外,当我重新检查框时,我得到了多个增量。以下是我的代码。我从我的类的OnCreate()方法调用testgps()方法。

如果您有任何建议,我们将不胜感激。

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


        }
    });
嘿,谢谢你的帮助。这就是我最终的结果。我在非访问修改器中挖了一些。

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


        }
    });

1 个答案:

答案 0 :(得分:3)

您可以添加一个删除侦听器的方法,如下所示:

private void removeMyLocationListener(LocationManager loc){
    loc.removeUpdates(your listener);
}

如果未按以下方式选中复选框,则会在OnCheckChangeListener内调用它:

chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(!isChecked){removeMyLocationListener(LocationManager loc);}
            }
        });