classBatteryManager(valreporter:KeyedAppStatesReporter){funlowBattery(battery:Int){reporter.setStatesImmediate(hashSetOf(KeyedAppState.builder().setKey("battery").setSeverity(KeyedAppState.SEVERITY_INFO).setMessage("Battery is low").setData(battery.toString()).build()))}}
Java
publicclassBatteryManager{privatefinalKeyedAppStatesReporterreporter;publicBatteryManager(KeyedAppStatesReporterreporter){this.reporter=reporter;}publicvoidlowBattery(intbattery){finalCollectionstates=newHashSet<>();states.add(KeyedAppState.builder().setKey("battery").setSeverity(KeyedAppState.SEVERITY_INFO).setMessage("Battery is low").setData(Integer.toString(battery)).build();reporter.setStatesImmediate(states);}}
[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Test app feedback\n\n| **Note:** To learn more about how to send feedback from your app, see [Send feedback to EMMs](/work/app-feedback/overview).\nAfter updating your app to support sending feedback in the form of keyed app states, you can use the guidance on this page to set up unit tests and send test feedback to a test device policy controller (DPC).\n\nSet up unit tests\n-----------------\n\nThis section provides examples of how to set up unit tests to check that your app interacts with\nkeyed app states as expected.\n\n### Step 1: Set up your classes to accept `KeyedAppStatesReporter` as a parameter\n\nInstead of calling `create()` directly, modify your classes to accept `KeyedAppStatesReporter` as a parameter like in the example `BatteryManager` class below: \n\n### Kotlin\n\n```kotlin\nclass BatteryManager(val reporter:KeyedAppStatesReporter) {\n fun lowBattery(battery:Int) {\n reporter.setStatesImmediate(\n hashSetOf(KeyedAppState.builder()\n .setKey(\"battery\")\n .setSeverity(KeyedAppState.SEVERITY_INFO)\n .setMessage(\"Battery is low\")\n .setData(battery.toString())\n .build()))\n }\n}\n```\n\n### Java\n\n```java\npublic class BatteryManager {\n private final KeyedAppStatesReporter reporter;\n public BatteryManager(KeyedAppStatesReporter reporter) {\n this.reporter = reporter;\n }\n\n public void lowBattery(int battery) {\n final Collection states = new HashSet\u003c\u003e();\n states.add(KeyedAppState.builder()\n .setKey(\"battery\")\n .setSeverity(KeyedAppState.SEVERITY_INFO)\n .setMessage(\"Battery is low\")\n .setData(Integer.toString(battery))\n .build();\n reporter.setStatesImmediate(states);\n }\n}\n```\n\nNext, use `KeyedAppStatesReporter.create` to get an instance to pass\nwherever `BatteryManager` is created.\n\n### Step 2: Add the enterprise feedback testing library to your `build.gradle` file\n\nAdd the following dependency to your app's\n[`build.gradle`](/studio/build#module-level) file: \n\n```\ndependencies {\n testImplementation 'androidx.enterprise:enterprise-feedback-testing:1.0.0'\n}\n```\n\n### Step 3: Create a `FakeKeyedAppStatesReporter` and pass it into your class\n\n### Kotlin\n\n```kotlin\nval reporter = FakeKeyedAppStatesReporter();\nval batteryManager = BatteryManager(reporter);\n```\n\n### Java\n\n```java\nFakeKeyedAppStatesReporter reporter = new FakeKeyedAppStatesReporter();\nBatteryManager batteryManager = new BatteryManager(reporter);\n```\n\n### Step 4: Assert interactions with `FakeKeyedAppStatesReporter`\n\n| **Note:** To view all the options for verifying expected interactions, see [`FakeKeyedAppStatesReporter`](https://developer.android.google.cn/reference/androidx/enterprise/feedback/FakeKeyedAppStatesReporter).\n\nFor example, to check that no states have been set: \n\n### Kotlin\n\n```kotlin\nassertThat(reporter.keyedAppStates).isEmpty();\n```\n\n### Java\n\n```java\nassertThat(reporter.getKeyedAppStates()).isEmpty();\n```\n\nOr that a particular state has been requested to be uploaded: \n\n### Kotlin\n\n```kotlin\nassertThat(reporter.uploadedKeyedAppStatesByKey[\"battery\"]).isNotNull()\n```\n\n### Java\n\n```java\nassertThat(reporter.getUploadedKeyedAppStatesByKey().get(\"battery\")).isNotNull();\n```\n\nSend test feedback to Test DPC\n------------------------------\n\nA sample [device policy controller](https://developers.google.com/android/work/terminology#device_policy_controller_dpc),\ncalled Test DPC, is capable of receiving app feedback and is available for\ndownload.\n\n### Step 1: Install Test DPC\n\nInstall the latest version of [Test DPC](https://play.google.com/store/apps/details?id=com.afwsamples.testdpc)\nfrom the Play Store. Next, set Test DPC as the admin of the device: \n\n```\nadb shell dpm set-device-owner com.afwsamples.testdpc/.DeviceAdminReceiver\n```\n\n### Step 2: Enable App feedback notifications\n\nIn Test DPC's menu, enable **App feedback notifications**.\n\nTrigger an event that set a keyed app state. If successful, Test DPC will show\nthe feedback in notifications:"]]