
Android
1. 在您的应用的AndroidManifest.XML文件中添加以下权限:

XML
2. 在您的应用中添加逻辑代码来请求地理位置权限。
您可以使用以下代码请求地理位置权限:
private void requestLocationPermission() {
// Check if the permission has been granted
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Request the permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION_PERMISSION);
} else {
// Permission has already been granted
// Do your location-related task here
}
}
3. 实现 onRequestPermissionsResult 函数处理用户响应。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_LOCATION_PERMISSION) {
// If the permission is granted, start your location-related task
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission has been granted
// Do your location-related task here
} else {
// Permission has been denied
Toast.makeText(this, "Location permission has been denied", Toast.LENGTH_SHORT).show();
}
}
}
通过以上设置,您的应用将可以访问设备的地理位置信息。
Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号