android.os.Process.killProcess(android.os.Process.myPid());//首先获取当前进程的id,然后杀死该进程
System.exit(0);//终止当前正在运行的Java虚拟机,让程序终止
//强制关闭与该包有关联的一切执行
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
manager.restartPackage(getPackageName());
//使用这种方式关闭应用程序需要加上权限
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
String currentProcess=getApplicationInfo().processName;
for (RunningAppProcessInfo appProcessInfo : appProcessInfos) {
String processName=appProcessInfo.processName;
if (processName.equals("com.lynhsp")) {
//System.out.println("ApplicationInfo-->"+processName);
android.os.Process.killProcess(appProcessInfo.pid);
//System.out.println("Killed -->PID:"+appProcessInfo.pid+"--ProcessName:"+processName);
}
}
//MyApplication类用来存储每一个activity,并实现关闭所有activity的操作
public class MyApplication extends Application {
//定义容activity容器
private List activityList = new LinkedList();
private static MyApplication instance;
private MyApplication(){}
//单例设计模式中取得唯一的MyApplication实例
public static MyApplication getInstance(){
if(instance == null)
instance = new MyApplication();
return instance;
}
//添加activity到容器中
public void addActivity(Activity activity){
activityList.add(activity);
}
//遍历所有的activity并finish
public void exitApp(){
for(Activity activity : activityList){
if(activity != null)
activity.finish();
}
System.exit(0);
}
//清空缓存
@Override
public void onLowMemory() {
super.onLowMemory();
System.gc();
}
}
//其次要在各个activity的onCreate方法中加入如下代码:
MyApplication.getInstance().addActivity(this);
//将该activity加入到activity容器中。
//最后,在实现退出应用程序的地方加入如下代码:
MyApplication.getInstance().exitApp();