Android管理API服务文件分配

Android management API service file allocation

我正在测试 android 管理 API 使用本地主机作为回调 url。我遵循了此 url Android Management API Sample 之后的每一步。 现在我被困在原地.. 根据本指南,我从服务帐户下载 json 文件。现在我复制那个 json 文件并保存在我项目的 app 文件夹中。 这是我的 enterprise.json 文件 Screenshot of json file in android studio

我只是在位置字符串中将文件夹位置指定为 enterprise.json 这是我的代码

private static final String PROJECT_ID = "enterprise-271814";
    private static final String SERVICE_ACCOUNT_CREDENTIAL_FILE =
            "enterprise.json";

    private static final String POLICY_ID = "samplePolicy";

    /** The package name of the COSU app. */
    private static final String COSU_APP_PACKAGE_NAME =
            "com.ariaware.devicepoliceycontroller";

    /** The OAuth scope for the Android Management API. */
    private static final String OAUTH_SCOPE =
            "https://www.googleapis.com/auth/androidmanagement";


    private static final String APP_NAME = "Device Policey Controller";

    private final AndroidManagement androidManagementClient;


    public Sample(AndroidManagement androidManagementClient) {
        this.androidManagementClient = androidManagementClient;
    }

    public void run() throws IOException {
        // Create an enterprise. If you've already created an enterprise, the
        // createEnterprise call can be commented out and replaced with your
        // enterprise name.
        String enterpriseName = createEnterprise();
        System.out.println("Enterprise created with name: " + enterpriseName);

        // Set the policy to be used by the device.
        setPolicy(enterpriseName, POLICY_ID, getCosuPolicy());

        // Create an enrollment token to enroll the device.
        String token = createEnrollmentToken(enterpriseName, POLICY_ID);
        System.out.println("Enrollment token (to be typed on device): " + token);

        // List some of the devices for the enterprise. There will be no devices for
        // a newly created enterprise, but you can run the app again with an
        // existing enterprise after enrolling a device.
        List<Device> devices = listDevices(enterpriseName);
        for (Device device : devices) {
            System.out.println("Found device with name: " + device.getName());
        }

        // If there are any devices, reboot one.
        if (devices.isEmpty()) {
            System.out.println("No devices found.");
        } else {
            rebootDevice(devices.get(0));
        }
    }

    public static AndroidManagement getAndroidManagementClient()
            throws IOException, GeneralSecurityException {
        try (FileInputStream input =
                     new FileInputStream(SERVICE_ACCOUNT_CREDENTIAL_FILE)) {
            GoogleCredential credential =
                    GoogleCredential.fromStream(input)
                            .createScoped(Collections.singleton(OAUTH_SCOPE));
            return new AndroidManagement.Builder(
                    GoogleNetHttpTransport.newTrustedTransport(),
                    JacksonFactory.getDefaultInstance(),
                    credential)
                    .setApplicationName(APP_NAME)
                    .build();
        }
    }

    private String createEnterprise() throws IOException {
        // Initiate signup process.
        System.out.println("Creating signup URL...");
        SignupUrl signupUrl =
                androidManagementClient
                        .signupUrls()
                        .create()
                        .setProjectId(PROJECT_ID)
                        .setCallbackUrl("https://localhost:9999")
                        .execute();
        System.out.print(
                "To sign up for a new enterprise, open this URL in your browser: ");
        System.out.println(signupUrl.getUrl());
        System.out.println(
                "After signup, you will see an error page in the browser.");
        System.out.print(
                "Paste the enterpriseToken value from the error page URL here: ");
        String enterpriseToken =
                new BufferedReader(new InputStreamReader(System.in)).readLine();

        // Create the enterprise.
        System.out.println("Creating enterprise...");
        return androidManagementClient
                .enterprises()
                .create(new Enterprise())
                .setProjectId(PROJECT_ID)
                .setSignupUrlName(signupUrl.getName())
                .setEnterpriseToken(enterpriseToken)
                .execute()
                .getName();
    }

    private Policy getCosuPolicy() {
        List<String> categories = new ArrayList<>();
        categories.add("android.intent.category.HOME");
        categories.add("android.intent.category.DEFAULT");

        return new Policy()
                .setApplications(
                        Collections.singletonList(
                                new ApplicationPolicy()
                                        .setPackageName(COSU_APP_PACKAGE_NAME)
                                        .setInstallType("FORCE_INSTALLED")
                                        .setDefaultPermissionPolicy("GRANT")
                                        .setLockTaskAllowed(true)))
                .setPersistentPreferredActivities(
                        Collections.singletonList(
                                new PersistentPreferredActivity()
                                        .setReceiverActivity(COSU_APP_PACKAGE_NAME)
                                        .setActions(
                                                Collections.singletonList("android.intent.action.MAIN"))
                                        .setCategories(categories)))
                .setKeyguardDisabled(true)
                .setStatusBarDisabled(true);
    }

    private void setPolicy(String enterpriseName, String policyId, Policy policy)
            throws IOException {
        System.out.println("Setting policy...");
        String name = enterpriseName + "/policies/" + policyId;
        androidManagementClient
                .enterprises()
                .policies()
                .patch(name, policy)
                .execute();
    }

    private String createEnrollmentToken(String enterpriseName, String policyId)
            throws IOException {
        System.out.println("Creating enrollment token...");
        EnrollmentToken token =
                new EnrollmentToken().setPolicyName(policyId).setDuration("86400s");
        return androidManagementClient
                .enterprises()
                .enrollmentTokens()
                .create(enterpriseName, token)
                .execute()
                .getValue();
    }

    private List<Device> listDevices(String enterpriseName) throws IOException {
        System.out.println("Listing devices...");
        ListDevicesResponse response =
                androidManagementClient
                        .enterprises()
                        .devices()
                        .list(enterpriseName)
                        .execute();
        return response.getDevices() ==null
                ? new ArrayList<Device>() : response.getDevices();

    }

    private void rebootDevice(Device device) throws IOException {
        System.out.println(
                "Sending reboot command to " + device.getName() + "...");
        Command command = new Command().setType("REBOOT");
        androidManagementClient
                .enterprises()
                .devices()
                .issueCommand(device.getName(), command)
                .execute();
    }

此外,我第一次使用 android 管理 api,我不知道它的正确实施。任何有这方面经验的人都可以指导我一点。我找到了很多关于此的内容,但我没有找到任何有用的教程

对于 Android,您必须将服务帐户文件存储在 assets 文件夹或 raw 文件夹中。

This thread 提供了有关根据您选择的位置将 json 数据加载到 InputStream 的多种方法的代码。