Q: Requesting Permissions at Run Time, When and Where?

I am writing a little app for android phone, which need to read files on external storage (SD). As from 6.0, you must Request this Permissions at Run Time.

I did this as the given APIs, but I found I must CALL readfile() at 2 places, how to improve to avoid this??

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


    if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
        else {

            LoadWords("testfile.txt");




        }




}

@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
    case 1: {
                if (grantResults.length > 0&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    LoadWords("testfile.txt");

                } else {
                    maxlines = 0;
                    return;
                }
                return;
            }

     }
}

wherever and whenever you want to read the file to be easy, you may like to use this, Permissions https://github.com/permissions-dispatcher/PermissionsDispatcher

You can use the lib : https://github.com/googlesamples/easypermissions

It is easily to use. I hope it can help your problem.

Create an abstract class like this with override method like this

public abstract class AbsRuntimePermission extends AppCompatActivity {
private SparseIntArray mErrorString;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mErrorString = new SparseIntArray();
}

public abstract void onPermissionsGranted(int requestCode);

public void requestAppPermissions(final String[]requestedPermissions, final int stringId, final int requestCode) {
    mErrorString.put(requestCode, stringId);

    int permissionCheck = PackageManager.PERMISSION_GRANTED;
    boolean showRequestPermissions = false;
    for(String permission: requestedPermissions) {
        permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
        showRequestPermissions = showRequestPermissions || ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
    }

    if (permissionCheck!=PackageManager.PERMISSION_GRANTED) {
        if(showRequestPermissions) {
            Snackbar.make(findViewById(android.R.id.content), stringId, Snackbar.LENGTH_INDEFINITE).setAction("GRANT", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActivityCompat.requestPermissions(AbsRuntimePermission.this, requestedPermissions, requestCode);
                }
            }).show();
        } else {
            ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
        }
    } else {
        onPermissionsGranted(requestCode);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    int permissionCheck = PackageManager.PERMISSION_GRANTED;
    for(int permisson : grantResults) {
        permissionCheck = permissionCheck + permisson;
    }

    if( (grantResults.length > 0) && PackageManager.PERMISSION_GRANTED == permissionCheck) {
        onPermissionsGranted(requestCode);
    } else {
        //Display message when contain some Dangerous permisson not accept
        Snackbar.make(findViewById(android.R.id.content), mErrorString.get(requestCode),
                Snackbar.LENGTH_INDEFINITE).setAction("ENABLE", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent();
                i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                i.setData(Uri.parse("package:" + getPackageName()));
                i.addCategory(Intent.CATEGORY_DEFAULT);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                startActivity(i);
            }
        }).show();
    }
}

}

and in the main

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hallfinder);

    requestAppPermissions(new String[]{
                    Manifest.permission.READ_CONTACTS,
                    Manifest.permission.READ_PHONE_STATE,},
            R.string.msg, REQUEST_PERMISSION);

}

Hope it will help you