我已遵循here的官方文档向用户显示位置对话框,因为我们的应用程序在没有位置的情况下无法正常工作。问题是,即使按对话框上的“确定”按钮,有时也会调用任务侦听器的onFailure,更重要的是,即使确实调用了onSuccess(LocationSettingsResponse locationSettingsResponse),我也无法立即获取位置详细信息,我所做的解决方法是创建一个单独的线程以在循环中获取详细信息。我需要知道一个更简单的方法来执行此操作。
这花了我超过2天的时间,但我仍然无法弄清楚代码中有什么问题。
public static void main(String[] args) {
Scanner readInput = new Scanner(System.in);
do {
calculate(readInput);
System.out.println("do you want to continue: y or n");
String userAnswer = readInput.next();
if(userAnswer.charAt(0) == 'y')
calculate(readInput);
else if(userAnswer.charAt(0) == 'n')
break;
} while (true);
readInput.close();
}
private static void calculate(Scanner readInput) {
double firstN;
double secondN;
char operator;
System.out.printf("Type a number, operator, number --" + "separated by a space: ");
firstN = readInput.nextDouble();
operator = readInput.next().charAt(0);
secondN = readInput.nextDouble();
if (operator == '+')
System.out.printf("%f + %f = %f", firstN, secondN, firstN + secondN);
else if (operator == '-')
System.out.printf("%f - %f = %f", firstN, secondN, firstN - secondN);
else if (operator == '*')
System.out.printf("%f * %f = %f", firstN, secondN, firstN * secondN);
else if (operator == '/')
System.out.printf("%f / %f = %f", firstN, secondN, firstN / secondN);
else if (operator == '%')
System.out.printf("%f %% %f = %f", firstN, secondN,firstN % secondN);
else
System.out.printf("Unknown operator");
System.out.printf("\n\n");
}
答案 0 :(得分:0)
尝试设置mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
就您的三星而言,在我的情况下它是有效的,但请记住,locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
在这种情况下将返回false。
答案 1 :(得分:0)
getLastLocation()
当至少有一个Google服务订阅者时有效。
您需要订阅,如果不需要不断更新坐标,则可以立即退订...
val client = LocationServices.getFusedLocationProviderClient(this)
client.requestLocationUpdates(locationRequest, object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
val location = locationResult?.lastLocation
//job.invoke()
client.removeLocationUpdates(this)
}
}, Looper.myLooper())