代码改变世界

Android完全关闭(退出)应用程序

2013-12-11 08:42  kingshow  阅读(2182)  评论(0编辑  收藏  举报

在一些android应用中,有时候,需要应用完全关闭,也就是把整个应用程序完全结束掉。一个简单的方法!

首先:

private void Exit() { 
     ActivityManager activityMgr= (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); 
     activityMgr.restartPackage(getPackageName()); 
} 

然后,

AndroidManifest.xml中添加对应权限。

<uses-permission android:name="android.permission.RESTART_PACKAGES" />
 
但是,
这个方法其实在2.2以前是可以的.
2.2以后使用的是:public void killBackgroundProcesses(String packageName)

skd:
public void restartPackage(String packageName)
This method is deprecated.
This is now just a wrapper for killBackgroundProcesses(String); the previous behavior here is no longer available to applications because it allows them to break other applications by removing their alarms, stopping their services, etc
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
其他的一些方法:

在网络上你可以找到一大堆的文章,每篇都是用 System.exit(0) 或者 android.os.Process.killProcess(android.os.Process.myPid()) 这两种方法!

但是有些时候, System.exit(0) 这个根本不行,而 android.os.Process.killProcess(android.os.Process.myPid()) 这个只能关闭当前的 Activity ,也就是对于一个只有单个 Activity 的应用程序有效,如果对于有多外 Activity 的应用程序它就无能为力了。

下面我介绍一下对于多个 Activity 的应用程序的完全关闭方法:

在 ActivityManager 类中提供了如下的方法:  

 /**

         * Have the system perform a force stop of everything associated with

         * the given application package. All processes that share its uid

         * will be killed, all services it has running stopped, all activities

         * removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED}

         * broadcast will be sent, so that any of its registered alarms can * be stopped, notifications removed, etc.

         *

         * You must hold the permission * {@link android.Manifest.permission#RESTART_PACKAGES} to be able to

         * call this method.

         *

         * @param packageName The name of the package to be stopped.

         */

    public void restartPackage(String packageName) {

    try {

        ActivityManagerNative.getDefault().restartPackage(packageName);

    }

    catch (RemoteException e) { }

 }

当然,这里说的restartPackage在2.2以后就过时了,2.2以后使用的是:public void killBackgroundProcesses(String packageName)!

所以如果要关闭整个应用程序的话只需要运行以下两行代码就行:

ActivityManager activityMgr= (ActivityManager) this.getSystemService(ACTIVITY_SERVICE );

activityMgr.restartPackage(getPackageName());

最后还需要添加这个权限才行:

<!-- 关闭应用程序的权限 -->
<uses-permission android:name="android.permission.RESTART_PACKAGES" />

 

多网友可能发现自己的Android程序有很多Activity,比如说主窗口A,调用了子窗口B,在B中如何关闭整个Android应用程序呢? 这里给大家三种比较简单的方法实现。

  首先要说明在B中直接使用finish(),接下来手机显示的还是主窗口A,所以一起来看看是如何实现的吧.

  1. Dalvik VM的本地方法

  android.os.Process.killProcess(android.os.Process.myPid())    //获取PID,目前获取自己的也只有该API,否则从/proc中自己的枚举其他进程吧,不过要说明的是,结束其他进程不一定有权限,不然就乱套了。
  System.exit(0);   //常规java、c#的标准退出法,返回值为0代表正常退出

 2. 任务管理器方法

   首先要说明该方法运行在Android 1.5 API Level为3以上才可以,同时需要权限android.permission.RESTART_PACKAGES,我们直接结束自己的package即可,直接使用ActivityManager类的restartPackage方法即可,参数为package name,该类通过getSystemService(Context.ACTIVITY_SERVICE)来实例化ActivityManager对象,这种方法系统提供的,但需要显示声明权限,所以使用中需要综合考虑。

 3. 根据Activity的声明周期

   我们知道Android的窗口类提供了历史栈,我们可以通过stack的原理来巧妙的实现,这里我们在A窗口打开B窗口时在Intent中直接加入标志 Intent.FLAG_ACTIVITY_CLEAR_TOP,这样开启B时将会清除该进程空间的所有Activity。

 在A窗口中使用下面的代码调用B窗口

Intent intent = new Intent();
intent.setClass(Android123.this, CWJ.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  //注意本行的FLAG设置
startActivity(intent);

 接下来在B窗口中需要退出时直接使用finish方法即可全部退出。