当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

kill_processes杀掉进程+启动进程

发表于: 2014-06-11   作者:baoyao   来源:转载   浏览次数:
摘要: MainActivity.java package com.kill_processes.test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.app.Acti
MainActivity.java
package com.kill_processes.test;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.app.ActivityManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
 private Button button_clear1,button_clear2,button_clear3,button_clear4,button_clear5;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        findViews();
        setListeners();
    }

    private void findViews() {
  // TODO Auto-generated method stub
        button_clear1=(Button) findViewById(R.id.button1);
        button_clear2=(Button) findViewById(R.id.button2);
        button_clear3=(Button) findViewById(R.id.button3);
        button_clear4=(Button) findViewById(R.id.button4);
        button_clear5=(Button) findViewById(R.id.button5);

 }

 private void setListeners() {
  // TODO Auto-generated method stub
   button_clear1.setOnClickListener(new OnClickListener() {   
    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     ActivityManager am=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
     am.restartPackage("com.canmera.test");
     Toast.makeText(MainActivity.this, "restartPackage", Toast.LENGTH_SHORT).show();
    }
   });
   //方法2. killBackgroundProcesses(getPackageName()); 坏处:kill掉得Service会自动重启
   button_clear2.setOnClickListener(new OnClickListener() {   
    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     ActivityManager am=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
     am.killBackgroundProcesses("com.canmera.test");
     Toast.makeText(MainActivity.this, "killBackgroundProcesses", Toast.LENGTH_SHORT).show();
    }
   });
   //反射调用forceStopPackage来结束进程
   button_clear3.setOnClickListener(new OnClickListener() {   
    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     ActivityManager am=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
      try {
                Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);   
                forceStopPackage.setAccessible(true);
                forceStopPackage.invoke(am, "com.canmera.test");//com.canmera.test是要结束进程的包名
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

      Toast.makeText(MainActivity.this, "forceStopPackage", Toast.LENGTH_SHORT).show();
      
    }
   });
   //执行Linux命令结束进程
   button_clear4.setOnClickListener(new OnClickListener() {   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Process p;
    int status = -1;
    try {
            p = Runtime.getRuntime().exec("killall -HUP com.canmera.test");//kill -9 com.canmera.test
      status = p.waitFor(); //status 0 indicator successed
      
      Toast.makeText(MainActivity.this, "status:"+status, Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  });
   
   //启动进程
   button_clear5.setOnClickListener(new OnClickListener() {   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Process p;
    int status = -1;
    try {
            p = Runtime.getRuntime().exec("am start -n com.canmera.test/com.canmera.test.MainActivity");
      status = p.waitFor(); //status 0 indicator successed
      
      Toast.makeText(MainActivity.this, "status:"+status, Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace(); 
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  });
 }

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

 



manifest.xml:

    <uses-permission android:name="android.permission.RESTART_PACKAGES"/><!-- 关闭其他应用 -->
    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/><!-- 关闭其他应用 -->
    <uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"/><!-- 强行关闭其他应用 -->

kill_processes杀掉进程+启动进程

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
linux服务器上,多少会出现一些僵尸进程,下面介绍如何快速寻找和消灭这些僵尸进程的方法 首先,我
转载:http://ywsm.iteye.com/blog/510670 http://58582786.blog.51cto.com/1550000/671487 目标:
转载:http://ywsm.iteye.com/blog/510670 http://58582786.blog.51cto.com/1550000/671487 目标:
转载:http://ywsm.iteye.com/blog/510670 http://58582786.blog.51cto.com/1550000/671487 目标:
转载:http://ywsm.iteye.com/blog/510670 http://58582786.blog.51cto.com/1550000/671487 目标:
转载:http://ywsm.iteye.com/blog/510670 http://58582786.blog.51cto.com/1550000/671487 目标:
9.4 SystemServer进程的启动 http://book.51cto.com/art/201109/291270.htm 2011-09-14 21:16 柯元
Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1047 ActivityThread.performLaunc
在windows下: 要让OBIEE起来,需要启动三个服务和一个oc4j Oracle BI presentation server 对应的
10 进程
进程 进程的两个基本要素:程序代码和数据集 当创建系统为一个进程的显式请求创建一个进程时叫做进
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号