private val coroutineScope = CoroutineScope(Dispatchers.IO)
private var isRunning = true
fun doBugReport(context: Context, cmd: String, callback: BugReportCallback? = null): Unit {
coroutineScope.launch {
try {
Logger.error(UploadUtils::class, "doBugReport开始")
val su = Runtime.getRuntime().exec("$cmd\n")
val outputStream = DataOutputStream(su.outputStream)
val inputStream = DataInputStream(su.inputStream)
val bufferedReader = BufferedReader(InputStreamReader(inputStream))
outputStream.flush()
Logger.error(UploadUtils::class, "doBugReport等待")
launch(Dispatchers.IO) {
while (isRunning) {
val result = bufferedReader.readLine()
Log.e("BugReport", "doBugReport: $result")
if (result.isNullOrEmpty())
break
if (result.containsIgnoreCase(END_STRING)) {
break
}
}
callback?.onResult(true)
inputStream.close()
}
su.waitFor()
outputStream.close()
Logger.error(UploadUtils::class, "doBugReport结束")
} catch (e: Exception) {
e.printStackTrace()
Logger.error(UploadUtils::class, "doBugReport异常: ${e.localizedMessage}")
}
}
}
评论