Setting permissions on Android

Kanzi Monitor requires privileges to write files and open TCP sockets. On Android, you must declare these permissions in the application manifest and grant them on the device.

Setting permissions in the manifest file

In the application manifest file, add permissions to write files and use TCP sockets.

For Android SDK version 30 and later:

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

For Android SDK versions earlier than 30:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

Setting permissions on the device

After installing the application on the target device, grant the application permission to write files. One practical way to do this is for the application to request permissions at startup.

For Android SDK version 30 and later:

void requestReadWritePermissions() {
    // Check and request permission to read and write to external storage.
    // Android SDK 30 and above requires MANAGE_EXTERNAL_STORAGE.
    if (SDK_INT >= Build.VERSION_CODES.R) {
        if (!Environment.isExternalStorageManager()) {
            Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
            Intent intent = new Intent(
                Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri);
            startActivity(intent);
        }
    }
}

For Android SDK versions earlier than 30:

void requestReadWritePermissions() {
    // Check and request permission to read and write to external storage.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED
            || checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE},
                0);
        }
    }
}

See also

Getting started with Kanzi Monitor