Below given is my Android java code. I have an Interface PermissionResultCallback
public interface PermissionResultCallback {
/**
* Called with the permission result.
*
* @param enabled {@link true} if the permissions have been granted, otherwise {@code false}.
*/
void onResult(boolean enabled);
}
I am calling this interface in startWithPermissionPrompt method
public void startWithPermissionPrompt(@Nullable final PermissionResultCallback callback) {
requestPermissionsTask = new RequestPermissionsTask(context.getApplicationContext(), new PermissionResultCallback() {
@Override
public void onResult(boolean enabled) {
if (enabled) {
//noinspection MissingPermission
startAdapter();
}
if (callback != null) {
callback.onResult(enabled);
}
}
});
requestPermissionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ACCESS_FINE_LOCATION);
}
The RequestPermissionTask is an Inner class which extends AsyncTask
private static class RequestPermissionsTask extends AsyncTask<String, Void, Boolean> {
@SuppressLint("StaticFieldLeak")
private final Context context;
private PermissionResultCallback callback;
RequestPermissionsTask(Context context, PermissionResultCallback callback) {
this.context = context;
this.callback = callback;
}
@Override
protected Boolean doInBackground(String... permissions) {
int[] result = HelperActivity.requestPermissions(context, permissions);
for (int element : result) {
if (element == PackageManager.PERMISSION_GRANTED) {
return true;
}
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (callback != null) {
callback.onResult(result);
}
}
}
Fortunately I have changed half of the equivalent code to C#. I know we can't change as exactly and directly to C#, but there may be some work arounds will be available. I have searched in all the forms but I can't able to find the exact solution.
Below I have added my Equivalent Xamarin android code for the above given Android java code.
Interface PermissionResultCallback
public interface IPermissionResultCallback
{
/**
* Called with the permission result.
*
* @param enabled {@link true} if the permissions have been granted, otherwise {@code false}.
*/
void onResult(Boolean enabled);
}
startWithPermissionPrompt method
public void StartWithPermissionPrompt(IPermissionResultCallback callback)
{
requestPermissionsTask = new RequestPermissionsTask(context, callback);
}
here the problem is I can't able to implement the onReceive interface method from IpermissionResultCallback interface
RequestPermissionsTask inner class
public class RequestPermissionsTask
{
public Context context;
public IPermissionResultCallback permissionResultCallback;
public RequestPermissionsTask(Context context, IPermissionResultCallback permissionResultCallback)
{
this.context = context;
this.permissionResultCallback = permissionResultCallback;
}
public async Task DoStuffInBackground()
{
await Task.Run(() =>
{
int[] result = HelperActivity.RequestPermissions(context, permissions);
foreach (var element in result)
{
if (element == (int)Permission.Granted)
{
return true;
}
}
return false;
});
}
}
In this inner class problem is I don't know how to pass params for HelperActivity.RequestPermissions(context, permissions); the permissions given here is automatically generated in android native code in doInBackground method.
I almost searched in all the forms and stackoverflow questions but I can't able to convert it.
Thanks in advance.