简体   繁体   中英

Android: Passing arguments when asking for permissions (Android 6)

in my Android app i need to ask for permissions to write on external storage. Since Android 6, you have to ask for the permissions at run-time.

My problem is, I don't know how to resume my origin task after asking for the permissions because my function got passed an argument (soundId) which is "lost" after asking for the permissions. How to pass arguments when asking for permissions ?

My code looks like this:

MainActivity.java

static final int REQ_CODE_EXTERNAL_STORAGE_PERMISSION = 45;

public static void sharesound(int soundId, final Context context){
if (ActivityCompat.checkSelfPermission(context,    Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
{
  CopyRAWtoSDCard(soundId, soundpath,context);
} 

else 
{
  ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQ_CODE_EXTERNAL_STORAGE_PERMISSION);
}
}



@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == REQ_CODE_EXTERNAL_STORAGE_PERMISSION && grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED )
    {
      sharesound(soundId,getApplicationContext()); **I cannot access soundId from here !** 
    }
}

EDIT:// This is outdated, better use ContentProvider or Fileprovider as no permissions are required for them!

I have found the solution by myself now:

first, we need a global variable:

 int lastsharedsound;

Then, the sharesound function saves the last shared sound into that variable.

public static void sharesound(int soundId)
{
    lastsharedsound=soundId;
    if (ActivityCompat.checkSelfPermission(context,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
    {
      CopyRAWtoSDCard(soundId, soundpath,context);
      ***do the intent sharing stuff here***
    } 

    else 
    {
      ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},     5);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) 
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == 5 && grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED )
    {
       sharesound(lastsharedsound); ***Here we call our function again*** 
    }
}
public static void sharesound(int soundId, final Context context){
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
{
  CopyRAWtoSDCard(soundId, soundpath,context);
} 

else 
{
  ActivityCompat.requestPermissions((Activity) context, new String[ {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQ_CODE_EXTERNAL_STORAGE_PERMISSION);
  if(ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
      sharesound(soundId, context);
  }
}
}

Edit: I added a check for the permission before calling the method again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM