彻底退出当前应用程序
http://263229365.iteye.com/blog/1283914
wifi 知识
http://smallwoniu.blog.51cto.com/3911954/1334951
像360一样杀死后台服务
http://my.oschina.net/mopidick/blog/277813#comments
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.example.android;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
/**
* 总结:杀死进程的几种方式。
*
* @author apple
*
*/
public class MainActivity_2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 方式一
android.os.Process.killProcess(android.os.Process.myPid());
// 方式二
ActivityManager am = (ActivityManager) this
.getSystemService(Context.ACTIVITY_SERVICE);
/*
* 该方法需要权限:<uses-permission
* android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
*/
am.killBackgroundProcesses("neta.oschina.app");
// 方式三,程序中调用shell命令。
// xml中加入button for test/
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "am force-stop ***",
Toast.LENGTH_LONG).show();
amForceAppProcess(getApplicationContext(), "net.oschina.app");
}
});
}
// +++++++++++++++++++++++++++++++
// 干掉进程的第三种方式。kill -9 pid
// 发送signal 查看linux signale命令
// +++++++++++++++++++++++++++++++
/**
* 该方法也告诉我们如何在代码中执行Linux shell命令。
*
* @param ctx
* @param packageName
*/
public void killProcessByShell(Context ctx, String packageName) {
int pid = getProcessID(ctx, packageName);
if (pid == -1) {
// pid未找到,没有继续执行的必要
return;
}
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(" kill -9 " + pid + " \n ");
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// +++++++++++++++++++++++++++++++
// 干掉进程的第四种方式。使用system/bin下的am命令: am force-stop packageName
// 发送signal 查看linux signale命令
// +++++++++++++++++++++++++++++++
/**
* 该方式最好,最彻底。
*
* @param ctx
* @param packageName
*/
public void amForceAppProcess(Context ctx, String packageName) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(" am force-stop " + packageName + " \n ");
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 通过包名获取进程ID
*
* @param packageName
* @return
*/
private int getProcessID(Context ctx, String packageName) {
// TODO Auto-generated method stub
ActivityManager am = (ActivityManager) ctx
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> infoList = am.getRunningAppProcesses();
int pid = -1;
if (infoList == null) {
return pid;
}
for (RunningAppProcessInfo info : infoList) {
if (info.processName
/* 通常是包名除非android:process配置 */.equals(packageName)) {
pid = info.pid;
}
}
return pid;
}
}