本文的内容摘自网络。
摘取本文的目的,并不是建议大家通过这种方式来结束应用,而是觉得,这种获取命令行返回内容的方式很不错,mark一下。
private void killProcess(Context mAct) {
// TODO Auto-generated method stub
Log.i(TAG, "killProcess");
String packageName = mAct.getPackageName();
String processId = "";
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec("ps");
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String inline;
while ((inline = br.readLine()) != null) {
if (inline.contains(packageName)) {
Log.i(TAG, "" + inline);
break;
}
}
br.close();
StringTokenizer processInfoTokenizer = new StringTokenizer(inline);
int count = 0;
while (processInfoTokenizer.hasMoreTokens()) {
count++;
processId = processInfoTokenizer.nextToken();
if (count == 2) {
break;
}
}
Log.e(TAG, "kill process : " + processId);
r.exec("kill -15 " + processId);
} catch (IOException ex) {
Log.e(TAG, "" + ex.getStackTrace());
}
}
mAct 是传入进来的一个全局变量,是Context,你直接做参数传进来就好了。
这个方法会把方法运行所在的程序全关掉,因为是通过找到该程序的进程ID,把整个进程KIll掉,所以可以直接关掉。
摘取本文的目的,并不是建议大家通过这种方式来结束应用,而是觉得,这种获取命令行返回内容的方式很不错,mark一下。
private void killProcess(Context mAct) {
// TODO Auto-generated method stub
Log.i(TAG, "killProcess");
String packageName = mAct.getPackageName();
String processId = "";
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec("ps");
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String inline;
while ((inline = br.readLine()) != null) {
if (inline.contains(packageName)) {
Log.i(TAG, "" + inline);
break;
}
}
br.close();
StringTokenizer processInfoTokenizer = new StringTokenizer(inline);
int count = 0;
while (processInfoTokenizer.hasMoreTokens()) {
count++;
processId = processInfoTokenizer.nextToken();
if (count == 2) {
break;
}
}
Log.e(TAG, "kill process : " + processId);
r.exec("kill -15 " + processId);
} catch (IOException ex) {
Log.e(TAG, "" + ex.getStackTrace());
}
}
mAct 是传入进来的一个全局变量,是Context,你直接做参数传进来就好了。
这个方法会把方法运行所在的程序全关掉,因为是通过找到该程序的进程ID,把整个进程KIll掉,所以可以直接关掉。