-2

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();
        }
    }
}
Flexo
  • 87,323
  • 22
  • 191
  • 272
proPes
  • 83
  • 8

1 Answers1

0

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!

  • thanks mauricio but I alredy have the code but when I implementde it in the onMenuItemClick() it gives me bunch of errors – proPes Dec 06 '18 at 13:54
  • @KarimAAkid Have you tried to replace `this` with `getContext()`? – Mauricio Flores Dec 06 '18 at 17:44
  • yes but the error still occure,it gives me : wrong first argument type ,Found android.content.context required: android.app.Activity – proPes Dec 06 '18 at 17:46
  • In that case add an extra parameter to the constructor of `WordAdapter` to hold a reference to the Activity where you are using this adapter. Then just use this reference instead of `this` – Mauricio Flores Dec 06 '18 at 20:27