如何停止在另一个活动从onLocationChanged()获取当前位置上按一下按钮在安卓
问题描述:
MainActivity.java如何停止在另一个活动从onLocationChanged()获取当前位置上按一下按钮在安卓
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
lat.setText(String.valueOf(latitude));
lon.setText(String.valueOf(longitude));
Toast.makeText(MainActivity.this, "location changed: " + latitude + " " + longitude, Toast.LENGTH_SHORT).show();
insertUser();
}
public void stopGetLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.removeUpdates(this);
}
在MainActivity.java,我得到的当前位置从onlocationchaged()和我”我写了一个名为stopGetLocation()的方法来禁用你的位置监听器。
StartJourney.java
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity m=new MainActivity();
m.stopGetLocation();
Toast.makeText(StartJourneyActivity.this, "JOURNEY STOPPED", Toast.LENGTH_SHORT).show();
}
});
在StartJourneyActivity.java,我创建了两个按钮,一个开始MainActivity和另一个停止收到的位置。但是当我尝试通过调用MainActivity.java的stopGetLocation()来停止获取位置时,我的应用程序强行关闭。 请给我解决方案,停止从按钮点击另一个活动获取位置。 在此先感谢.. !!
尝试使用接口 – Saveen