android - close app if user not granting WRITE_EXT

2019-07-10 17:02发布

问题:

in android 6 or above when user open app first time ask him for some permission if he dont allow then exit app

some must required permission for my app is read and write to external storage, how to achieve this

i tried this

if (Build.VERSION.SDK_INT >= 23) {
                if (ContextCompat.checkSelfPermission(LoginActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(LoginActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(LoginActivity.this,
                            new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE},
                            1);
                } else {
                    //do something
                }
            } else {
                    //do something
            }

回答1:

You need to read the results onRequestPermissionsResult() and if the user refused the permissions, call context.finish() or context.finishAffinity(). Something like... Edit: Take a look on the docs to learn how to handle the permissions: Android 6 Permissions

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        switch (requestCode) {
            case MAIN_PERMISSIONS:
                if (grantResults.length > 0) {
                    // Validate the permissions result
                    if (permissionsOk) {
                        // All good
                    } else {
                        // Close your app
                        closeNow();
                    }
                }
                break;
        }
    }

    private void closeNow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            finishAffinity();
        } else {
            finish();
        }
    }


回答2:

using suggestion from @G. Brisida - Lince

i solved it and i am writing my answer here for other peoples if they need same thing,

this all code i write in MainActivity:

    if(Build.VERSION.SDK_INT >= 23)
            {
                if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                                                        != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
                                                        != PackageManager.PERMISSION_GRANTED)
                {
     ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE},
                                1);
    }
                else
                {
                    //do something
                }
            }
}

and this is override method onRequestPermissionsResult:

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch(requestCode)
        {
            case 1:
                if (grantResults.length > 0  && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                     // i am creating folder in external storage here
                 File create_dir=new File(Environment.getExternalStorageDirectory()+"/uca/");

                 Toast.makeText(this,"creating",Toast.LENGTH_LONG).show();
                 create_dir.mkdirs();

                }
                else
                {
                    closeNow();
                }
                break;
        }
    }

this is close now mthod :

private void closeNow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        {
            finishAffinity();
        }

        else
        {
            finish();
        }
    }