【问题标题】:Alway failed ACCESS_FINE_LOCATION permission without asking for permissionACCESS_FINE_LOCATION 权限总是在没有请求权限的情况下失败
【发布时间】:2017-12-14 15:14:45
【问题描述】:

我想使用 setMyLocationEnabled(true)。但由于权限问题无法使用。

我的 android 应用程序没有请求 ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION 的权限。未经许可,总是无法获得许可。

测试设备Android版本:6.0.1
编译 SDK 版本和目标 SDK:API-25,Android 7.1.1
构建工具版本:25.0.3
最低 SDK 版本:API-22,Android 5.1

AndroidManifest.xml 中添加了以下权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permisson.READ_GSERVICES"/>
<uses-permission android:name="edu.bloomu.huskies.tsc71523.skatelogger.MAPS_RECEIVE"/>

MapsActivity.java:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    LatLng sydney = new LatLng(0, 0);
    mMap.addMarker(new MarkerOptions().position(sydney).title("TestLocation"));

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        Log.e("LOCATION", "ACCESS_FINE_LOCATION-------Successful");
        mMap.setMyLocationEnabled(true);
    }
    else {
        Log.e("LOCATION", "ACCESS_FINE_LOCATION-------Failed");
    }

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        Log.e("LOCATION", "ACCESS_COARSE_LOCATION-------Successful");
        mMap.setMyLocationEnabled(true);
    }
    else{
        Log.e("LOCATION", "ACCESS_COARSE_LOCATION-------Failed");

    }
}
}

【问题讨论】:

  • 您可能在请求权限对话框中选中了“不再询问”标志,尝试清除数据。而且您没有请求许可
  • 安装“Google Play 服务”后就可以使用了。谢谢。

标签: android google-maps-android-api-2


【解决方案1】:

从 Android Marshmallow (6.0) 开始,如果您的目标是 API 级别 23 或更高级别,则必须使用新的权限模型(请求权限)。

本次讲座将帮助您了解新模型:https://youtu.be/WGz-alwVh8A

此帮助程序库还可以使其更易于使用并无缝支持旧版本的 Android:https://github.com/hiqes/andele

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2012-12-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多