try {
Runtime rnTm=Runtime.getRuntime();
Process execLang = rnTm.exec(new String[]{"getprop", "persist.sys.language"});
mCurrLocale = new BufferedReader(new InputStreamReader (execLang.getInputStream())).readLine();
execLang.destroy();
Process execCountry = rnTm.exec(new String[]{"getprop", "persist.sys.country"});
mCurrCountry = new BufferedReader(new InputStreamReader (execCountry.getInputStream())).readLine();
execLang.destroy();
Log.e("", "Device locale: "+mCurrLocale+" Co:"+mCurrCountry);
} catch (IOException e) {
e.printStackTrace();
return;
}
catch (SecurityException se) {
se.printStackTrace();
return;
}
It's working fine some phones and tablet.
但是有一段时间它导致我的应用冻结并在logcat中提供以下结果。
I/System ( 1511): Failed to destroy process 1547
I/System ( 1511): libcore.io.ErrnoException: kill failed: ESRCH (No such process)
I/System ( 1511): at libcore.io.Posix.kill(Native Method)
I/System ( 1511): at libcore.io.ForwardingOs.kill(ForwardingOs.java:77)
I/System ( 1511): at java.lang.ProcessManager$ProcessImpl.destroy(ProcessManager.java:257)
答案 0 :(得分:0)
如果它在某些设备上运行而在其他设备上运行则意味着您必须在异常中捕获它并使程序保持静默。
我建议在代码下面添加一个带有泛型异常的catch:
catch (Exception ge) {
ge.printStackTrace();
return;
}
答案 1 :(得分:0)
我看到了这种解决方法(我没试过)
https://groups.google.com/forum/#!topic/android-developers/VcjXWBosQ88
也许它可以帮到你
finally {
destroyProcess(process);
}
private static void destroyProcess(Process process) {
try {
if (process != null) {
// use exitValue() to determine if process is still running.
process.exitValue();
}
} catch (IllegalThreadStateException e) {
// process is still running, kill it.
process.destroy();
}
}