Ryan
Ryan

Reputation: 747

Android: Delay startup until permissions granted

So I'm new to Android development and have a basic application that writes to a text file on it's first time loading. But because it is the application's first time loading it must first ask the user for read/write permissions. The problem I have is that the application tries to write the file before it has these permissions. I have the following MainActivity.java class that contains the section of code that requests for permissions:

public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Stes fullscreen and removes title
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

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

        setContentView(new Environment(this));
    }
}

The constructor for Environment.jar then calls a function that writes the file if it does not yet exist.

But it seems that while the application does request permissions, it doesn't wait to receive these permissions before starting again. Is there any way to get it to wait to recieve the permissions, and if the permissions are not received to close the application?

Upvotes: 1

Views: 3158

Answers (2)

Kapil G
Kapil G

Reputation: 4141

You should call your Environment constructor only if the Permission is granted. Otherwise you can show an error dialog asking user to first grant permission before proceeding with the app. To check for permission You should request for permission as below -

Bool isPermissionGiven = false;
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission. READ_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");
        isPermissionGiven =  true;
    } else {

        Log.v(TAG,"Permission is revoked");
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        isPermissionGiven =  false;
    }

if (isPermissionGiven){
    new Environment(this)
}

Then implement onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
    case 0:
        boolean isPerpermissionForAllGranted = false;
        if (grantResults.length > 0 && permissions.length==grantResults.length) {
            for (int i = 0; i < permissions.length; i++){
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
                    isPerpermissionForAllGranted=true;
                }else{
                    isPerpermissionForAllGranted=false;
                }
            }

            Log.e("value", "Permission Granted, Now you can use local drive .");
        } else {
            isPerpermissionForAllGranted=true;
            Log.e("value", "Permission Denied, You cannot use local drive .");
        }
        if(isPerpermissionForAllGranted){
            // Do your work here
            new Environment(this);
        }
        break;
}

}

Upvotes: 2

LanternMike
LanternMike

Reputation: 664

you should implment onRequestPermissionsResult() method more here https://developer.android.com/training/permissions/requesting.html

Also its a violation of the android rules to terminate the app like that. You should just print something like this app requires this permission to run. Or whatever you want to say and do nothing more if you want but don't terminate the app.

Also be prepared to launch the app no matter what. You can stick in place holder text while you wait like "loading..." and on a successful result update the text.

Upvotes: 0

Related Questions