【问题标题】:Stop sending GPS coordinates Android停止发送 GPS 坐标 Android
【发布时间】:2017-01-13 19:52:22
【问题描述】:

单击按钮 (mapbttn) 后,我的谷歌地图应用程序每 5 秒发送一次坐标。我想制作另一个按钮来停止发送这些坐标。我该怎么做?

这是我的 .java 文件:

    private GoogleMap mMap;
    Button mapbttn;
    private LocationManager locationManager;
    private LocationListener locationListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        mapbttn = (Button) findViewById(R.id.setRangeButton);

        mapbttn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                btncoord();
            }
        });

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Toast.makeText(getApplicationContext(), "" + location.getLatitude() + '\n' + location.getLongitude(), Toast.LENGTH_LONG).show();
            }
            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }
            @Override
            public void onProviderEnabled(String s) {

            }

            public void onProviderDisabled(String s) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        };

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.INTERNET
                }, 10);
                return;
            } else {
                btncoord();
            }
        }
        Bundle save = getIntent().getExtras();

        if (save != null) color = save.getString("marker color");
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 10:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    btncoord();
                return;
        }
    }

    public void btncoord() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 5000, 0, locationListener);
    }

【问题讨论】:

    标签: java android google-maps android-studio gps


    【解决方案1】:

    只需取消注册您的位置管理器,例如

    locationManager.removeUpdates(ActivityName.this);

    docs

    【讨论】:

    • 注意:你也应该在你的活动的 onDestroy 中这样做,否则它不会停止
    • 我很高兴@AndreeaMateescu
    【解决方案2】:

    试试这个

    public void stopUpdates(){
        locationManager.removeUpdates(context);
    }
    

    在停止按钮上调用stopUpdate()方法。

    【讨论】:

    • 你为什么要发布一个已经在这里的答案。还有很多其他未解答的问题,您可以尝试。保持道德
    • @PavneetSingh 正如您在 SO 上看到的那样,一个问题有多个答案。当我写答案时,你赢得了比赛。我会根据作者的需要详细回答。
    • 你的意思是你不想通过采取其他不道德的例子来保持道德,再加上至少给出一个有效的论据来证明你的立场,否则就不会给。很有趣,有这么多的阐述
    • @PavneetSingh 我没有足够的知识与你争论。当我将我的答案与您的答案进行比较时,您的答案是特定于活动的,它只能在 Activity 类中使​​用
    • 谢谢哥们,我也不喜欢无用的争论,我只建议,所以也鼓励编辑作为建议,你应该使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    相关资源
    最近更新 更多