7

这是我正在使用的以下代码

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)

有什么解决办法吗?

4

2 回答 2

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();
    }
}
于 2014-05-09T10:09:17.500 回答
0

如果它适用于某些设备而不适用于其他设备,则意味着您必须在异常中捕获它并保持程序静默。

我建议在您的代码下方添加一个带有通用异常的捕获:

catch (Exception ge) {
 ge.printStackTrace();
 return;
 }
于 2014-01-29T16:49:37.463 回答