gpt4 book ai didi

java - onLocationChanged() 不会自动调用

转载 作者:行者123 更新时间:2023-12-01 09:20:33 28 4
gpt4 key购买 nike

如果我理解正确,在这段代码中,我的代码 onLocationChanged 方法应该每 1m 或 0.4sec 自动调用一次,因为我设置了 locationManager.requestLocationUpdates(provider, 400, 1 ,这个);

但是什么也没发生!当我按下按钮时,onLocationChanged 被调用。没有任何内容自动调用,无论是每 1 秒还是 0.4 秒。有人可以帮忙吗?

public class MainActivity extends AppCompatActivity implements LocationListener {
LocationManager locationManager;
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
String provider;

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

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

provider = locationManager.getBestProvider(new Criteria(),false);

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;
}
Location location = locationManager.getLastKnownLocation(provider);

if (location != null) {

Log.i("Location Info", "Location achieved!");
Toast.makeText(getApplicationContext(),"Location achieved!",Toast.LENGTH_SHORT).show();

} else {

Log.i("Location Info", "No location :(");
Toast.makeText(getApplicationContext(),"No location :(",Toast.LENGTH_SHORT).show();

}

}
public boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission. ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission. ACCESS_FINE_LOCATION)) {

// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Text")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission. ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
})
.create()
.show();


} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission. ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
} else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// permission was granted, yay! Do the
// location-related task you need to do.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission. ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {

//Request location updates:
locationManager.requestLocationUpdates(provider, 400, 1, this);
}

} else {

// permission denied, boo! Disable the
// functionality that depends on this permission.

}
return;
}

}
}



@Override
protected void onResume() {
super.onResume();

checkLocationPermission();


}

@Override
protected void onPause() {
super.onPause();

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

}

@Override
public void onLocationChanged(Location location) {

Double lat = location.getLatitude();
Double lng = location.getLongitude();

Log.i("Location info: Lat", lat.toString());
Log.i("Location info: Lng", lng.toString());
Toast.makeText(getApplicationContext(),"Location info: Lat"+lat.toString(),Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"Location info: Lng"+lng.toString(),Toast.LENGTH_SHORT).show();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}


public void getLocation(View view) {

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;
}
Location location = locationManager.getLastKnownLocation(provider);

onLocationChanged(location);
}
}

最佳答案

如果已授予权限,只需修改您的 onResume() 方法重写即可请求位置更新。

如果尚未授予权限,则 requestLocationUpdates() 调用将在 onRequestPermissionsResult() 方法重写中发生(如果用户接受权限请求)。

如果之前已授予该权限,您可以立即调用 requestLocationUpdates()

@Override
protected void onResume() {
super.onResume();

if (checkLocationPermission()) {
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
}

关于java - onLocationChanged() 不会自动调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40194455/

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