从Asynctask调用requestPermissions

时间:2016-02-19 10:33:29

标签: android android-fragments android-permissions

我正在尝试从AsyncTask调用的android.support.v4.app.Fragment请求权限。

On doInBackground方法有这段代码

     int hasWriteContactsPermission = ContextCompat.checkSelfPermission((Context)ctx,
                Manifest.permission.SEND_SMS);

        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale((AppCompatActivity)ctx,
                    Manifest.permission.SEND_SMS)) {
                showMessageOKCancel("Necessary permissions", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                requestPermissions(new String[]{Manifest.permission.SEND_SMS},
                                        REQUEST_CODE_ASK_PERMISSIONS);
                            }
                        });
                return;
            }

           /******** PROBLEM
     requestPermissions(new String[] {Manifest.permission.SEND_SMS},
                    REQUEST_CODE_ASK_PERMISSIONS);

*************/
            return;
        }

通过AppCompatActivity调用requestPermissions对我无效,因为我需要在onRequestPermissionsResult中捕获Fragment,而不是Activity

问题是requestPermissions说出了这个错误:

Cannot resolve method requestPermissions 

如何从requestPermissions调用Asynctask方法来捕获调用者片段的结果?

2 个答案:

答案 0 :(得分:4)

实际上你不能在doInBackground方法中请求权限,因为顾名思义,它正在backgorund中工作,并且向用户请求权限需要使用UI线程。我建议您调用这样的请求权限:

    public void checkPermissions() {
    final String[] permissions = new String[]{Manifest.permission.SEND_SMS}

    if ( ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.SEND_SMS )
            != PackageManager.PERMISSION_GRANTED ) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                android.Manifest.permission.SEND_SMS )){

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Permission Needed");
            builder.setMessage("You must accept the permission.");
            builder.setPositiveButton(R.string.global_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    ActivityCompat.requestPermissions(ConfigurarActivity.this,
                            permissions,
                            PERMISSION_REQUEST);
                }
            });
            builder.setNegativeButton(R.string.global_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
        } else {
            ActivityCompat.requestPermissions(this,
                    permissions,
                    PERMISSION_REQUEST);
        }
    }else{
        //doTaskThatNeedThePermission can be async
    }
}

答案 1 :(得分:-2)

如果您有活动参考

,则可以执行requestPermissions
// in your Activity
final Activity act = this;

// onClick
act.requestPermissions(new String[]{Manifest.permission.SEND_SMS},
    REQUEST_CODE_ASK_PERMISSIONS);