gpt4 book ai didi

Android 检查 LocationManager 的权限

转载 作者:IT老高 更新时间:2023-10-28 13:07:13 26 4
gpt4 key购买 nike

当我点击我的 Activity 布局中的一个按钮时,我试图让 GPS 坐标显示。以下是单击按钮时调用的方法:

public void getLocation(View view) {
TextView tv = (TextView) findViewById(R.id.gps_coord_view);
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
tv.setText("Latitude: " + loc.getLatitude() + "\nLongitude: " + loc.getLongitude());
}

我收到一条错误提示

Call requires permission which may be rejected by user. Code should explicitly check to see if permission is available.

我已经在我的 AndroidManifest 中授予了这些权限。当我在调用 lm.getLastKnownLocation 之前添加以下内容时,该错误已得到解决,并且应用程序编译:

if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}

但是,当我按下调用 getLocation 的按钮时,应用程序会崩溃。到底是怎么回事?是否有更好/更简单的方法来获取设备的 GPS 坐标?

最佳答案

对于 Android API 级别 (23),我们需要检查权限。 https://developer.android.com/training/permissions/requesting.html

我遇到了同样的问题,但以下方法对我有用,并且我能够成功检索位置数据:

(1) 确保您拥有 list 中列出的权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

(2) 确保您向用户请求权限:

if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {

ActivityCompat.requestPermissions( this, new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION },
LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION );
}

(3) 确保使用 ContextCompat,因为它与旧 API 级别兼容。

(4) 在你的位置服务,或者初始化你的 LocationManager 并获取最后一个已知位置的类中,我们需要检查权限:

if ( Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return ;
}

(5) 这种方法仅在我将 @TargetApi(23) 包含在我的 initLocationService 方法的顶部之后才对我有效。

(6) 我也将这个添加到我的 gradle 构建中:

compile 'com.android.support:support-v4:23.0.1'

这是我的 LocationService 供引用:

public class LocationService implements LocationListener  {

//The minimum distance to change updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 10 meters

//The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 0;//1000 * 60 * 1; // 1 minute

private final static boolean forceNetwork = false;

private static LocationService instance = null;

private LocationManager locationManager;
public Location location;
public double longitude;
public double latitude;


/**
* Singleton implementation
* @return
*/
public static LocationService getLocationManager(Context context) {
if (instance == null) {
instance = new LocationService(context);
}
return instance;
}

/**
* Local constructor
*/
private LocationService( Context context ) {

initLocationService(context);
LogService.log("LocationService created");
}



/**
* Sets up location service after permissions is granted
*/
@TargetApi(23)
private void initLocationService(Context context) {


if ( Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return ;
}

try {
this.longitude = 0.0;
this.latitude = 0.0;
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

// Get GPS and network status
this.isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
this.isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (forceNetwork) isGPSEnabled = false;

if (!isNetworkEnabled && !isGPSEnabled) {
// cannot get location
this.locationServiceAvailable = false;
}
//else
{
this.locationServiceAvailable = true;

if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
updateCoordinates();
}
}//end if

if (isGPSEnabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateCoordinates();
}
}
}
} catch (Exception ex) {
LogService.log( "Error creating location service: " + ex.getMessage() );

}
}


@Override
public void onLocationChanged(Location location) {
// do stuff here with location object
}
}

到目前为止,我仅使用 Android Lollipop 设备进行了测试。希望这对你有用。

关于Android 检查 LocationManager 的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32491960/

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