如何向用户询问WRITE_SETTINGS和WRITE_EXTERNAL_STORAGE权限

时间:2018-12-06 00:18:42

标签: android android-studio permissions runtime-permissions

希望有人可以帮助我,所以我正在开发一个铃声应用程序,我需要在用户单击菜单项时设置WRITE_SETTINGS和WRITE_EXTERNAL_STORAGE权限(设置默认铃声,警报...)

这是我的自定义适配器代码:

...............

PopupMenu popup = new PopupMenu(getContext(),more);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {

//I think I should write the permissions code here 

int ringtone = currentWord.getAudioResourceId();

..............

这是我得到错误的地方: 输入'this'引起了我尝试将其更改为Mainactivity.this的问题,但仍然不起作用

public class WordAdapter extends ArrayAdapter<Word>  {


public WordAdapter(Context context, ArrayList<Word> words) {
    super(context, 0, words);

}

private int STORAGE_PERMISSION_CODE = 2;
private RelativeLayout rl ;


private void requestStoragePermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) || (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.READ_EXTERNAL_STORAGE))) {

        new AlertDialog.Builder(getContext())
                .setTitle("Permission needed")
                .setMessage("This permission is needed because of this and that")
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions(com.example.android.miwok.MainActivity,
                                new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
                    }
                })
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .create().show();

    } else {
        ActivityCompat.requestPermissions(WordAdapter.this,
                new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == STORAGE_PERMISSION_CODE)  {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我个人建议您使用TedPermission  或Dexter询问用户权限。它具有简单的界面,结果很简单,被授予或被拒绝。

Ted权限可以与RxAndroid一起使用,这可能会有用。

这是您的选择,祝您的应用程序好运!