Android “onRequestPermissionsResult” not being called

限于喜欢 提交于 2019-12-20 02:54:21

问题


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.

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

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

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

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

Any ideas?

This is my activity:

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.
        }
    }
}

}


回答1:


MY_LOCATION_REQUEST_CODE should be a non-egative constant.

You should declare it as

private static final int MY_LOCATION_REQUEST_CODE = 1;



回答2:


This line will never evaluate to true:

permissions[0] == android.Manifest.permission.ACCESS_FINE_LOCATION

Because you are comparing these two strings by reference.

You should use TextUtils.equals()

TextUtils.equals(permissions[0], android.Manifest.permission.ACCESS_FINE_LOCATION)



回答3:


I have the same issue, the issue was only on API 23 (Android M). And that because I was using the flag android:noHistory="true" in the manifest when declaring the activity:

<activity   android:name=".view.activity.SplashScreenActivity"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

That because when showing the permission dialog the activity will be finished by the system because of that flag.

Removing it, or setting it to false fixes my issue.



来源:https://stackoverflow.com/questions/39298129/android-onrequestpermissionsresult-not-being-called

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!