Android" onRequestPermissionsResult"没被打电话 [英] Android "onRequestPermissionsResult" not being called

查看:151
本文介绍了Android" onRequestPermissionsResult"没被打电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现Marshmallow的权限支持,但我的代码onRequestPermissionsResult永远不会被调用。代码的onCreate和onMapReady部分工作正常。检查设备是否具有权限并请求权限正常。

I'm trying to implement Marshmallow's permission support, but my code inside "onRequestPermissionsResult" is never called. The "onCreate" and "onMapReady" parts of the code work fine. Checking if the device has the permission and requesting the permission works fine.

我正在使用运行Android Marshmallow 6.0,API 23的Nexus 5X模拟器。

I'm using an Nexus 5X emulator running Android Marshmallow 6.0, API 23.

我试图做这里提到的 - > Android M Permissions:onRequestPermissionsResult()未被调用

I have tried to do what it's mentioned here -> Android M Permissions: onRequestPermissionsResult() not being called

但是我无法让它工作,如果我使用ActivityCompat也没关系.requestPermissions或直接requestPermissions,它会被调用。

But I cannot get it to work, it doesn't matter if I use "ActivityCompat.requestPermissions" or directly "requestPermissions", it nevers get called.

我还将我的appcompat和支持库更新到v24.2.0

I also updated my appcompat and support libraries to v24.2.0

任何想法?

这是我的活动:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
UiSettings mapSettings;
int MY_LOCATION_REQUEST_CODE;

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

    //Check location permission for sdk >= 23
    if (Build.VERSION.SDK_INT >= 23) {

        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
        } else {
            // Request permission.
            ActivityCompat.requestPermissions(MapsActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_LOCATION_REQUEST_CODE);
        }
    }

}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == MY_LOCATION_REQUEST_CODE) {
        if (permissions.length == 1 &&
                permissions[0] == android.Manifest.permission.ACCESS_FINE_LOCATION &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
        } else {
            // Permission was denied. Display an error message.
        }
    }
}

}

推荐答案

MY_LOCATION_REQUEST_CODE 应为非正常常数。

您应该将其声明为

private static final int MY_LOCATION_REQUEST_CODE = 1;

这篇关于Android" onRequestPermissionsResult"没被打电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆