【问题标题】:Android - Prompting / requesting permissions at startupAndroid - 在启动时提示/请求权限
【发布时间】:2019-01-13 10:08:07
【问题描述】:

我正在尝试获取一个弹出窗口,请求用户在启动期间授予访问外部存储的权限。我有一个名为 Start 的活动,这是我的 Launcher 活动。通过此活动,我声明了一些全局变量并创建了一些目录(如果不存在)。为此,我需要对外部存储的权限。我已经实现了在 developer.android.com 上共享的代码,但它没有授予我所需的权限。

我认为这不起作用,因为请求用户输入的弹出窗口没有显示,因为 layout.activity_wait 还不可见,因此我应该将我的代码分发到 AsyncTask 中,以便 layout.activity_wait 在调用 requestPermissions 之前可见。

下面的代码没有 AsyncTask(是的,我知道应用程序也应该能够在没有权限的情况下工作,我将在稍后阶段构建它:))。请让我知道您对此有何看法。谢谢!

public class Start extends AppCompatActivity {

    /**
    set a bunch of variables to be used in all activities
    **/

    public static ...
    public static ... ...
    public static int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 0;



    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wait);

        checkPermission();


        /**
        do some stuff at startup, like creating some directories if not already present
        **/

        launchIntent(this, Home.class); //launch the home screen after initializing all variables and creating directories
        finish();
    }


    private void checkPermission() {
        if (ContextCompat.checkSelfPermission(Start.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            Log.d("permission", "NOT granted");
             ActivityCompat.requestPermissions(Start.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
        }else{
            Log.d("permission", "granted");
        }
    }
}

【问题讨论】:

  • 权限请求对话框异步显示。也就是说,当您调用 checkPermission() 并且需要显示对话框时 - 即,当您还没有权限时 - onCreate() 中的执行不会在发生这种情况时暂停。它会立即继续,并尝试做你的“事情”,不管你是否真的有许可。您需要覆盖Activity 中的onRequestPermissionsResult() 方法才能知道权限对话框何时完成。

标签: java android permissions


【解决方案1】:

感谢@Mike M. 设法让它工作。我不明白 onRequestPermissionsResult() 的目的。现在我愿意。请参阅下面的代码。

public class Start extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

        /**
        set a bunch of variables to be used in all activities
        **/

        public static ...
        public static ... ...
        public static int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 0;



        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_wait); //this is a view with a progress bar
            checkPermission();

        }

        private void permissionsGranted() {

                 /**
            do some stuff at startup, like creating some directories if not already present
            **/

            launchIntent(this, Home.class); //launch the home screen after initializing all variables and creating directories
            finish();


        }



        private void checkPermission() {
            if (ContextCompat.checkSelfPermission(Start.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                // do this if permisisons have not yet been granted
                Log.d("permission", "NOT granted");
                ActivityCompat.requestPermissions(Start.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
            }else{
                // if the permissions have already been granted do the following
                Log.d("permission", "granted");
                permissionsGranted();
            }
        }

        // this is required when ActivityCompat.requestPermissions() is called
        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
            switch (requestCode) {
                case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {

                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                       // if permissions have been granted, do the following 
                        permissionsGranted();
                    } else {
                        // possibly prompt why permissions are required and try again
                    }
                    return;
                }

                // other 'case' lines to check for other
                // permissions this app might request.
            }
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 2015-10-08
    • 2017-07-25
    相关资源
    最近更新 更多