android - 无法使用 ADB Espresso 清除应用数据
问题描述
无法使用以下方法清除应用程序数据,我使用的是有根设备 API 19。
public static void clearAppData() {
try {
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes(pm clear com.xxx.xxx.xxx);
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.wait(2000);
} catch (Exception e) {
Log.e("Set Time", e.getMessage());
}
}
我在测试中调用了上述方法,但它无法清除缓存。我已经为更改设备时间做了同样的事情,并且它有效
在以下测试中调用上述方法:
@Test
public void a_test() {
CommonUtil.clearAppData();
CommonUtil.changetime();
CommonUtil.pausetime(5000);
}
解决方案
这与以下解决方案一起使用,跳过了 adb 命令并创建了以下方法
public static void clearSharedPrefs(Context context) {
SharedPreferences sharedPreferences = context.
getSharedPreferences(KEY_SP_PACKAGE, Context.MODE_PRIVATE);
// sharedPreferences.edit().remove("KEY").commit(); //Remove Key
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();
}
推荐阅读
- java - 从 Gradle 构建生成 BOM
- apache-nifi - ApacheNiFi 处理器 updateAttribute 运行了太多任务,但我只有一个流文件传输到它
- ios - iOS 13 语音控制辅助功能 - 有没有办法进行更多自定义操作(而不仅仅是链接到辅助功能标签)?
- dart - 如何在 dart /flutter 中格式化数字/货币
- ios - Xcode 错误:0x0 的内存读取失败,没有堆栈
- python - boto3 ec2中的分页描述实例
- python - 如何创建循环的东西?
- javascript - 脚本在 Wordpress 中的某些页面上不起作用
- c# - OmniSharp 扩展无法在 Arch VScode 上加载
- ios - 在后台运行应用程序的最佳方式是什么