Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime Permission at Startup

I have a flashlight that needs CAMERA and WRITE_SETTINGS permissions, I need to first handle these permissions which I already know how to do it, Then if it is granted, for example, start my main activity... (sorry, I am new in android)

like image 483
S.R Avatar asked Mar 12 '23 17:03

S.R


2 Answers

Add this library from github in your gradle file

Than add this code

PermissionListener permissionlistener = new PermissionListener() {
    @Override
    public void onPermissionGranted() {
        //The user have conceded permission
        Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPermissionDenied(ArrayList<String> deniedPermissions) {
        //close the app or do whatever you want
        Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
    }
};

new TedPermission(this)
.setPermissionListener(permissionlistener)
.setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
.check();

Or if you prefer do it natively, check in the google docs

like image 191
diogojme Avatar answered Mar 16 '23 11:03

diogojme


On your manifest file add permissions:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />

On your main activity java file inside "onCreate" add following code

    String[] PERMISSIONS = new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.CALL_PHONE};
    if (
                    (ContextCompat.checkSelfPermission(MapsActivity.this, PERMISSIONS[0]) != PackageManager.PERMISSION_GRANTED) ||
                    (ContextCompat.checkSelfPermission(MapsActivity.this, PERMISSIONS[1]) != PackageManager.PERMISSION_GRANTED)
            )
    {
        ActivityCompat.requestPermissions(MapsActivity.this, PERMISSIONS, REQUEST_INTERNET);
        finish();
    }
like image 41
Krishan Liyanaarachchi Avatar answered Mar 16 '23 11:03

Krishan Liyanaarachchi