`

如何退出Android应用程序

阅读更多

android 退出应用程序因为,android Activity的特殊性,导致想要完整的关闭一个由多个activity组成的应用程序是非常困难的,如果,activity调用方式错综复杂,那就更难完整的关闭了。以我的理解,在android里一个Activity就等同于一个独立的应用程序,所以,即便你认为几个Activity合起来才算是一个整体的应用程序,但android不会这么认为。所以,你会发现使用System.exit(0);来关闭应用程序,只会关闭当前显示的Activity,不会关闭所有的Activity。
我看了一些帖子,他们说设计一个应用程序时,最好由一个Activity调用所有的Activity,不要嵌套的调用。
下面给出关闭Activity的两种方法。
1). System.exit(0);
2). android.os.Process.killProcess(android.os.Process.myPid());
以上只是个人观点,在这次做项目时得出的一些结论,可能不太准确,还待修改。
在开发一个android应用软件时,考虑如何关闭应用程序的问题。一开始采用当前Activity调用finish()来完成,但这种做法是不对的。 这几天通过阅读Android的frameworks层代码时,发现平台提供了关闭程序的方法。 在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) { } }

ok,我们只需要取得ActivityManager的实例,并调用该方法即可正常关闭应用。 以下是关闭程序的代码片段:

private void exit2() {

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

actMgr.restartPackage(getPackageName()); }

调用上述方法就能彻底地关闭掉程序,一般仅仅调用finish()是无法彻底关闭程序的。 但要注意一点,调用系统服务需要在AndroidManifest.xml文件中增加权限。SDK2.1后就被废弃了,因为这个方法会关闭同其他应用程序共享的Service,

 

 

 

1.int version = android.os.Build.VERSION.SDK_INT;

                            ActivityManager activityMgr = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);

                            if (version <= 7) {

                                   activityMgr.restartPackage(mContext.getPackageName());

                            } else {

                                   mContext.stopService(new Intent(mContext, MusicService.class));

//                                 activityMgr.killBackgroundProcesses(mContext.getPackageName());

//需要权限KILL_BACKGROUND_PROCESSES

                                   android.os.Process.killProcess(pid);

                            }

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

 

2.int pid = android.os.Process.myPid();

android.os.Process.killProcess(pid);   //杀死当前进程

3.this.finish()     thisà当前activity   //杀死当前进程

 

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

  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窗口    //返回Home界面,貌似可行。

Intent intent  = new Intent(Intent.ACTION_MAIN);

 intent.addCategory(Intent.CATEGORY_HOME);

  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  startActivity(intent);

  System.exit(0);

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

 

4:自己写一个Application,在内部自己管理Acitvity,与版本无关,绝对可以。

 


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics