Menu

170825Ago - CHAPTER 6

170825Ago - CHAPTER 6

Here again!!!

Today I go to add to the my app the permissions check. It's because I'll need the convenient permissions from the user to access to the device storage, then I'll can offer to the user the file browser functionality.

I'm developing for a SDK level of 23, that's a Marshmallow Android version 6.0. This is indicated to Android Studio in the file "build.Gradle (Module app)" with:

minSdkVersion 23

This mean that the app can't run in devices more old. Note that this is not a problem because Marshmallow has a 23% of the market and the newer version (Nougat) a 13%.

Precisely in this SDK version the user permissions system change in the Android API.

In previous versions, the permissions are given in the installation moment and to revoke was necessary to uninstall the app.

But from Marsmallow (API 23), we must ask to the user for permissions in the moment of start the app, and is not necessary ask again for them. But the user can change the permissions granted in any moment from the Settings/Application of Android.

To check permissions on run time we must change our omCreate methos of MainActivity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkPermissions();

    enableFunctionalities();

    [...]
    }

    private void checkPermissions(){
        Log.v(TAG, "Requering permissions...");
        ArrayList<String> listPermissionsNeeded = new ArrayList<>();
        listPermissionsNeeded.add(Manifest.permission.CAMERA);
        listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        ActivityCompat.requestPermissions(this,
                listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
                REQUEST_ID_MULTIPLE_PERMISSIONS);
    }

When the user give permissions the event onRequestPermisionsResult is called:

public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults){
        Log.v(TAG, "Request code: "+requestCode);
        for (int i=0; i<permissions.length; i++){
            Log.v(TAG, "Permissions "+permissions[i]+" was "+
                    ((grantResults[i]== PackageManager.PERMISSION_GRANTED) ? "GRANTED" : "DENIED"));
        }

        // Functionalities not implemented yet. For future...
        if ( (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED)
            && (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED)){
            Log.v(TAG, "Enabling File Browser functionality...");
            Button btn=(Button) findViewById(R.id.btn_files);
            btn.setEnabled(true);
        }
        if ( checkSelfPermission(Manifest.permission.CAMERA)==PackageManager.PERMISSION_GRANTED){
            Log.v(TAG, "Enabling Pictures functionality...");
            Button btn=(Button) findViewById(R.id.btn_pictures);
            btn.setEnabled(true);
        }
    }

When the app starts, the log output shows us:

08-25 13:20:13.708 26912-26912/homebrew.dhtejada.androidblog.mysuperapp V/MainActivity_1.25: Request code: 1
08-25 13:20:13.708 26912-26912/homebrew.dhtejada.androidblog.mysuperapp V/MainActivity_1.25: Permissions android.permission.CAMERA was GRANTED
08-25 13:20:13.708 26912-26912/homebrew.dhtejada.androidblog.mysuperapp V/MainActivity_1.25: Permissions android.permission.WRITE_EXTERNAL_STORAGE was GRANTED
08-25 13:20:13.708 26912-26912/homebrew.dhtejada.androidblog.mysuperapp V/MainActivity_1.25: Permissions android.permission.READ_EXTERNAL_STORAGE was GRANTED
08-25 13:20:13.708 26912-26912/homebrew.dhtejada.androidblog.mysuperapp V/MainActivity_1.25: Enabling File Browser functionality...
08-25 13:20:13.718 26912-26912/homebrew.dhtejada.androidblog.mysuperapp V/MainActivity_1.25: Enabling Pictures functionality...

Well, that's all for today... Is Friday! Is Friday! Is Friday!!!

At the app start we'll be asked for permissions:

We can to revoke in any moment the permissions in Android/Settings for the app:

Special thanks to http://imghst.co/ for the image hosting!

Posted by David Hernandez Tejada 2017-08-25 Labels: ANDROID STUDIO ANDROID DEVELOPMENT PROGRAMMING USER PERMISSIONS

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.