• 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
Nov 3, 2015
198
29
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);
            }
        }
    }
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones