如何将Android Java代码转换为同等的C#Xamarin Android代码
下面给出的是我的Android Java代码。我有一个接口 cormissionResultCallback
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);
}
我在 startwithpermission prompt 方法中称此接口方法
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);
}
requestPermissionTask 是一个内部类,它扩展了 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);
}
}
}
幸运的是,我将同等代码的一半更改为C#。我知道我们不能直接更改为C#,但是可能会有一些工作可用。我已经以所有表格进行了搜索,但是我找不到确切的解决方案。
下面,我为上述给定的Android Java代码添加了等效的Xamarin Android代码。
接口许可ResultCallback
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);
}
startWithPermistromptrompt 方法
public void StartWithPermissionPrompt(IPermissionResultCallback callback)
{
requestPermissionsTask = new RequestPermissionsTask(context, callback);
}
这里的问题是我无法实现 inReceive 接口方法
requestPermissionStask 在此内类问题中的内部类
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;
});
}
}
是我不知道如何通过 helperactivity.requeStperpermissions(上下文,权限); 此处给出的权限自动生成doinbackground方法中的Android本机代码。
我几乎用所有表格和堆叠式问题搜索,但我无法将其转换。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论