如何使用DevicePolicyManager.ClearApplicationUserData

How to use DevicePolicyManager.ClearApplicationUserData

我正在用 c#(在 visual studio 环境中)编写设备所有者应用程序,我需要创建清除其他应用程序数据的函数。 我用谷歌搜索了如何在 C# 中使用 DevicePolicyManager.ClearApplicationUserData,但我发现的唯一有用的东西是:

private void clearApplicationUserData(String packageName) {
    mDevicePolicyManager.clearApplicationUserData(mAdminComponentName, packageName, new MainThreadExecutor(), (__, succeed) -> showToast(succeed ? R.string.clear_app_data_success : R.string.clear_app_data_failure, packageName));
}

(来源:https://github.com/googlesamples/android-testdpc/blob/master/app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java ;线路:3382-3391)

我将其从 java 转换为 C# 并收到此错误: 错误 CS1660 无法将 lambda 表达式转换为类型 'DevicePolicyManager.IOnClearApplicationUserDataListener' 因为它不是委托类型

public static void ClearApplicationUserData(string pkg)
        {
            IExecutor executor = Executors.NewSingleThreadExecutor();
            devicePolicyManager.ClearApplicationUserData(Admin, pkg, executor, (strPKG,boolSUCCESS) => Log.Debug("ClearApplicationUserData", boolSUCCESS ? "SUCCESS" : "FAIL"));
        }

看起来应该可行,但我不知道如何使 'listener' 非空

public static void ClearApplicationUserData(string pkg)
        {
            IExecutor executor = Executors.NewSingleThreadExecutor();
            DevicePolicyManager.IOnClearApplicationUserDataListener listener;
            devicePolicyManager.ClearApplicationUserData(Admin, pkg, executor, listener);
        }

我试过:

devicePolicyManager.ClearApplicationUserData(Admin, pkg, executor, (DevicePolicyManager.IOnClearApplicationUserDataListener)((strPKG, boolSUCCESS) => Log.Debug("ClearApplicationUserData", "-")));

错误 CS1660 无法将 lambda 表达式转换为类型 'DevicePolicyManager.IOnClearApplicationUserDataListener',因为它不是委托类型

devicePolicyManager.ClearApplicationUserData(Admin, pkg, executor, (Action<DevicePolicyManager.IOnClearApplicationUserDataListener>)((strPKG, boolSUCCESS) => Log.Debug("ClearApplicationUserData", "-")));

错误 CS1593 委托 'Action<DevicePolicyManager.IOnClearApplicationUserDataListener>' 不接受 2 个参数

devicePolicyManager.ClearApplicationUserData(Admin, pkg, executor, (Action<DevicePolicyManager.IOnClearApplicationUserDataListener>)((AAAA) => Log.Debug("ClearApplicationUserData", "-")));

错误 CS1503 参数 4:无法从 'System.Action<Android.App.Admin.DevicePolicyManager.IOnClearApplicationUserDataListener>' 转换为 'Android.App.Admin.DevicePolicyManager.IOnClearApplicationUserDataListener?'

devicePolicyManager.ClearApplicationUserData(Admin, pkg, executor, delegate { (DevicePolicyManager.IOnClearApplicationUserDataListener)((AAAA) => Log.Debug("ClearApplicationUserData", "-")); });

错误 CS1660 无法将匿名方法转换为类型 'DevicePolicyManager.IOnClearApplicationUserDataListener' 因为它不是委托类型

可能有帮助:

https://developer.android.com/reference/android/app/admin/DevicePolicyManager#clearApplicationUserData(android.content.ComponentName,%20java.lang.String,%20java.util.concurrent.Executor,%20android.app.admin.DevicePolicyManager.OnClearApplicationUserDataListener)

https://developer.android.com/reference/android/app/admin/DevicePolicyManager.OnClearApplicationUserDataListener

[Register("clearApplicationUserData", "(Landroid/content/ComponentName;Ljava/lang/String;Ljava/util/concurrent/Executor;Landroid/app/admin/DevicePolicyManager$OnClearApplicationUserDataListener;)V", "GetClearApplicationUserData_Landroid_content_ComponentName_Ljava_lang_String_Ljava_util_concurrent_Executor_Landroid_app_admin_DevicePolicyManager_OnClearApplicationUserDataListener_Handler", ApiSince = 28)]
public virtual void ClearApplicationUserData(ComponentName? admin, string? packageName, IExecutor? executor, IOnClearApplicationUserDataListener? listener);
....
....
....
[Register("android/app/admin/DevicePolicyManager$OnClearApplicationUserDataListener", "", "Android.App.Admin.DevicePolicyManager/IOnClearApplicationUserDataListenerInvoker", ApiSince = 28)]
        public interface IOnClearApplicationUserDataListener : IJavaObject, IDisposable, IJavaPeerable
        {
            [Register("onApplicationUserDataCleared", "(Ljava/lang/String;Z)V", "GetOnApplicationUserDataCleared_Ljava_lang_String_ZHandler:Android.App.Admin.DevicePolicyManager/IOnClearApplicationUserDataListenerInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", ApiSince = 28)]
            void OnApplicationUserDataCleared(string? packageName, bool succeeded);
        }

        public class ClearApplicationUserDataEventArgs : EventArgs
        {
            public ClearApplicationUserDataEventArgs(string? packageName, bool succeeded);

            public string? PackageName { get; }
            public bool Succeeded { get; }
        }

这个有效

public class ClearApplicationUserDataListener : Java.Lang.Object, DevicePolicyManager.IOnClearApplicationUserDataListener
{
    public void OnApplicationUserDataCleared(string packageName, bool succeeded) { }
}
public static void ClearApplicationUserData(string pkg)
{
    IExecutor executor = Executors.NewSingleThreadExecutor();
    var listener = new ClearApplicationUserDataListener();
    devicePolicyManager.ClearApplicationUserData(Admin, pkg, executor, listener);           
}