使用 AndroidManagement API 在托管设备上安装应用程序的策略主体

Policy body to install an Application on the Managed device using AndroidManagement API

我正在 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
}

并创建策略主体实例:

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; }
}

我怎样才能做到这一点?

谢谢!

使用 C# 库时,您不能直接 "load" JSON,您需要使用 C# 语义构建 Patch method takes a 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#,所以这段代码可能无法编译,但希望这能给你思路。