How to ask user for WRITE_SETTINGS and WRITE_EXTER

2019-03-07 02:19发布

问题:

hop someone could help me , so I'm developing a ringtone app and I need to ask the user for WRITE_SETTINGS and WRITE_EXTERNAL_STORAGE permissions when he click on menu items (set default ringtone , alarme ...)

here is my code for the custom adapter :

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

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 is where I get errors : the input 'this' causes the problem I tried to change it to Mainactivity.this but still doesn't work

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:

Personally I would recommend you to use TedPermission or Dexter for asking the user for permissions. It has a simple interface and the result is straightforward, granted or denied.

Ted Permissions can be used along with RxAndroid which you might find useful.

It's your choice, good luck with your app!