• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps checkSelfPermission() is not working ???

ac4android

Well-Known Member
This bit of code should work, it checks the SDK version and whether ACCESS_FINE_LOCATION permission has been granted in the Manifest.
But I am getting this error: Cannot resolve method 'checkSelfPermission(java.lang.String)'
The permissions in Manifest:
Code:
   <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
I think the "android.permission.ACCESS_COARSE_LOCATION" is unnecessary as ACCESS_FINE_LOCATION would automagically include the coarse location... just a thought.

The code snippet that's not working:
// I am compiling on SDK22 because I want to test various pieces of codes, this being one of them

Code:
import java.util.ArrayList;

    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= 23 && !isPermissionRequested) {

            isPermissionRequested = true;

            ArrayList<String> permissions = new ArrayList<>();
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
            }

            if (permissions.size() == 0) {
                return;
            } else {
                requestPermissions(permissions.toArray(new String[permissions.size()]), 0);
            }
        }
    }
 
I am not sure with the snippet you provided, but I suspect your activity extends the Activity class. If you compile with SDK 22, the method checkSelfPermission(...) is not available from Activity. You should extend ActivityCompat to have features from the SDK 23 available while compiling against an older SDK.
 
Oh, I see.

Based on that article, the app might have granted the "dangerous" permission in the Manifest but it might have been revoked since, hence the checkSelfPermission() every time.

I wouldn't have thought ACCESS_FINE_LOCATION is dangerous.
 
Back
Top Bottom