gpt4 book ai didi

java - 停止发送 GPS 坐标 Android

转载 作者:行者123 更新时间:2023-11-29 19:34:47 26 4
gpt4 key购买 nike

在我点击按钮 (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);
}

最佳答案

只需取消注册您的位置管理器即可

locationManager.removeUpdates(ActivityName.this);

docs

关于java - 停止发送 GPS 坐标 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39347595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com