想以编程方式使用“dumpsys meminfo”打印出应用程序在某些时间点的内存使用情况,
添加<uses-permission android:name="android.permission.DUMP" />
,并使用以下代码在模拟器上进行测试,但出现异常:“java.io.IOException:无法运行程序“adb”:错误 = 13,权限被拒绝”
fun dump() {
val process = Runtime.getRuntime().exec("adb shell dumpsys meminfo com.testapp.demo")
process.waitFor()
val bufferedReader = BufferedReader(InputStreamReader(process.getInputStream()))
var buffer: String? = ""
while (bufferedReader.readLine().also({ buffer = it }) != null) {
Log.i("+++", buffer)
}
buffer = ""
val buffered = BufferedReader(InputStreamReader(process.getErrorStream()))
while (buffered.readLine().also({ buffer = it }) != null) {
Log.w("+++", buffer)
}
}