0

我正在 C# 控制台应用程序中使用 Google 的 AndroidManagement API 构建一个 MDM 应用程序,并且我还为设备创建了一个具有默认值的策略。

我正在尝试创建一个应用程序策略以在设备上安装一个新的应用程序包,所有博客都建议在 JSON 中添加应用程序策略:

{
  "applications": [
    {
      "packageName": "com.example",
      "installType": "FORCE_INSTALLED",
      "lockTaskAllowed": true,
      "defaultPermissionPolicy": "GRANT"
    },
    {
      "packageName": "example2",
      "installType": "FORCE_INSTALLED",
      "lockTaskAllowed": false,
      "defaultPermissionPolicy": "GRANT"
    },
    {
      "packageName": "example3",
      "installType": "FORCE_INSTALLED",
      "lockTaskAllowed": true,
      "defaultPermissionPolicy": "GRANT"
    }
  ],
  "cameraDisabled": true,
  "screenCaptureDisabled": true,
  "adjustVolumeDisabled" :  true
}

并创建了 Policy body 实例:

public static Google.Apis.AndroidManagement.v1.Data.Policy body = new Google.Apis.AndroidManagement.v1.Data.Policy();

Json 加载完美,但由于类型转换application policy lists而无法保存。ApplicationPolicy

{
    var appPolicy = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText(applicationPolicyJson));

    policyBody.CameraDisabled = appPolicy.CameraDisabled;
    policyBody.ScreenCaptureDisabled = appPolicy.ScreenCaptureDisabled;
    policyBody.AdjustVolumeDisabled = appPolicy.AdjustVolumeDisabled;

    policyBody.Applications = new List<ApplicationPolicy> {***appPolicy*** };
}


public class Rootobject
{
   public Application[] Applications { get; set; }
   public bool CameraDisabled { get; set; }
   public bool ScreenCaptureDisabled { get; set; }
   public bool AdjustVolumeDisabled { get; set; }
}
public class Application
{
   public string PackageName { get; set; }
   public string InstallType { get; set; }
   public bool LockTaskAllowed { get; set; }
   public string DefaultPermissionPolicy { get; set; }
}

我怎样才能做到这一点?

谢谢!

4

1 回答 1

0

使用 C# 库时,您无法直接“加载” JSON,Patch方法采用您需要使用 C# 语义构建的Policy对象。Java 也是如此。另一方面,Python 库采用易于从 JSON 生成的字典。

对于 C#,这看起来像:

using Google.Apis.AndroidManagement.v1.Data;

var policy = new Policy();

policy.CameraDisabled = true;
policy.ScreenCaptureDisabled = true;
policy.AdjustVolumeDisabled = true;

var app1 = new ApplicationPolicy();
app.PackageName = "com.android.chrome";
app.InstallType = "FORCE_INSTALLED";
app.LockTaskAllowed = true;
app.DefaultPermissionPolicy = "GRANT";

var app2 = new ApplicationPolicy();
app.PackageName = "com.example";
app.InstallType = "FORCE_INSTALLED";

policy.Applications = new List<ApplicationPolicy> {app1, app2};

免责声明:我不习惯 C#,所以这段代码可能无法编译,但希望这能给你这个想法。

于 2019-04-05T11:36:37.587 回答