问:在运行时请求权限,何时何地?

时间:2017-10-09 02:44:41

标签: java android

我正在为Android手机编写一个小应用程序,它需要读取外部存储(SD)上的文件。从6.0开始,您必须在运行时请求此权限。

我做了这个给定的API,但是我发现我必须在2个地方调用readfile(),如何改进以避免这个?

@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;
            }

     }
}

2 个答案:

答案 0 :(得分:0)

您可以使用lib:https://github.com/googlesamples/easypermissions

这是一个有权限的示例活动:

SfMultiColumnDropDown

易于使用。 我希望它可以帮助你解决问题。

答案 1 :(得分:0)

使用像这样的覆盖方法创建这样的抽象类

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();
    }
}

}

并在主

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);

}

希望它能帮到你