我试了两种方法:
- Java code
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);List<RunningAppProcessInfo> procList = null;int result=-1;procList = activityManager.getRunningAppProcesses();for (Iterator<RunningAppProcessInfo> iterator = procList.iterator(); iterator.hasNext();) { RunningAppProcessInfo procInfo = iterator.next(); if(procInfo.processName.equals("com.android.phone")) { try { //activityManager.restartPackage(procInfo.processName); activityManager.killBackgroundProcesses(procInfo.processName); } catch (Exception e){ } break; }}
以及
- Java code
try { java.lang.Process process = Runtime.getRuntime().exec("kill -9 " + phoneId); // Waits for the command to finish. process.waitFor();} catch (IOException e) { e.printStackTrace();} catch (InterruptedException e) { throw new RuntimeException(e);}
两种方法都没能成功的kill 掉 phone.
请问这是为什么呢?难道phone不允许被kill? 进adb shell 可以kill的呀。
------解决方案--------------------
android.os.Process.killProcess(android.os.Process.myPid());
关闭应用程序