android 杀进程方法

关闭应用的方法:

1.System.exit(0);

2.android.os.Process.killProcess(android.os.Process.myPid()); ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

3. am.killBackgroundProcesses("packagename");

4. am.restartPackage("packagename"); 以上方法貌似只能退出本身应用。

强退其他应用: 利用shell 下面的的kill命令来杀掉进程从而达到退出应用的效果。kill processid; 此命令需要root权限执行 直接上代码

1.  ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
  List<RunningAppProcessInfo> infos = am.getRunningAppProcesses();
  for(RunningAppProcessInfo info : infos){
   Log.i("---",info.processName);
   if("com.test.br2".equals(info.processName)){
    Log.i(TAG, info.processName+"---"+info.pid);
//    Process.killProcess(info.pid);
    killProcess(info.pid);
    break;
   }
  }
2.         killProcess1("packageName");
 private void killProcess(int processId){
  java.lang.Process process = null;
  try {
         process = Runtime.getRuntime().exec("su");
            OutputStream os = process.getOutputStream();
            os.write(("kill "+processId).getBytes());
            os.flush();
            os.close();
            Thread.sleep(3000) //这里的sleep的目的让上面的kill命令执行完成
        } catch (IOException ex) {
            Log.e(TAG, "" + ex.getStackTrace());
        }finally{
         if(process!=null){
          process.destroy();
          process =null;
         }
        }
 }
    private void killProcess1(String packageName) {
        Log.i(TAG, "killProcess");
        java.lang.Process process = null;
        try {
         String processId = "";
         process = Runtime.getRuntime().exec("su");
            OutputStream os = process.getOutputStream();
            os.write("ps \n".getBytes());
            os.flush();
            BufferedReader br = new BufferedReader(new InputStreamReader(process
                    .getInputStream()));
            String inline;
            while ((inline = br.readLine()) != null) {
                if (inline.contains(packageName)) {
                    Log.i(TAG, "" + inline);
                    StringTokenizer processInfoTokenizer = new StringTokenizer(inline);
                    int count = 0;
                    while (processInfoTokenizer.hasMoreTokens()) {
                        count++;
                        processId = processInfoTokenizer.nextToken();
                        if (count == 2) {
                            break;
                        }
                    }
                    Log.i(TAG, "kill process : " + processId);
                    os.write(("kill " + processId).getBytes());
                    os.flush();
                    if(os!=null){
                  os.close();
                  os =null;
                 }
                    br.close();
                    return;
                }
            }
        } catch (IOException ex) {
            Log.e(TAG, "" + ex.getStackTrace());
        }finally{
         if(process!=null){
          process.destroy();
          process =null;
         }
        }
    }

你可能感兴趣的:(android)