简体   繁体   中英

How to grant permissions programmatically

In the below code, I am checking the availability and if the permissions are granted in the manifest file by the developer. In case the permission was not already granted in the manifest, as stated in the code it should be programatically granted and added to the manifest "or as far I know".

I removed the three permissions from the manifest and then started the app. However, the app crashed. I expected that it should run as the missing permissions would have been granted automatically/programmatically.

please let me know how the below code can be amended so that that objective can be achieved.

**code:

String[] PermissionsLocation =
        {
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.INTERNET
        };

private void checkPermissions() {
    int iter = 0;
    for (String p : PermissionsLocation) {
        if (ContextCompat.checkSelfPermission(this, p) != getPackageManager().PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, PermissionsLocation, 101 + iter);
        }
        ++iter;
    }
}

You need to declare the permissions in the manifest file first before you request for them programmatically.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.snazzyapp">

    <uses-permission android:name="android.permission.INTERNET"/> 
    <!-- other permissions go here -->

    <application ...>
        ...
    </application>
</manifest>

refer this for more information.

Check it out, if it helps

if(ContextCompat.checkSelfPermission(mActivity,Manifest.permission.INETRNET) == PackageManager.PERMISSION_GRANTED){
                    // Internet permission granted
                    Toast.makeText(mContext,"Permission granted.",Toast.LENGTH_SHORT).show();
                }else {
                    // Internet permission not granted
                    Toast.makeText(mContext,"Permission not granted.",Toast.LENGTH_SHORT).show();
                }
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_select_language);
    requestPermissions();

}

private void requestPermissions() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(getApplicationContext(),
                Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
                 requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CALL_PHONE,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.READ_LOGS,
                    Manifest.permission.SEND_SMS,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.READ_SMS,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.INTERNET,
                    Manifest.permission.RECEIVE_SMS


            }, 0);
        }
    }

There are 2 kinds of permission 1) Normal 2) Dangerous

You don't need to ask permission for Normal Permission.

As per documents

"Permissions are divided into several protection levels. The protection level affects whether runtime permission requests are required. "

Refere docLink

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM