简体   繁体   English

Android:位置监听器

[英]Android: Location Listener

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. 对于测试,如果我在10秒内移动了1米以上,我只想将计数器增加1。

I am able to get the location listener to work and the checkbox onclick listener to work. 我能够使位置侦听器正常工作,并使复选框onclick侦听器正常工作。 However, I am having issues trying to remove the locationlistener when the box is not checked. 但是,当未选中此框时,尝试删除locationlistener时遇到问题。 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. 我从类的OnCreate()方法中调用testgps()方法。

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() { 私人无效的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: 如果您的复选框未选中,请在OnCheckChangeListener内部调用它:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM