OSC android app 退出方法改進

/**
 * 應用程序Activity管理類:用於Activity管理和應用程序退出
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class AppManager {
    .......html

/**
     * 退出應用程序
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
               activityMgr.killBackgroundProcesses(context.getPackageName());            //activityMgr.restartPackage(context.getPackageName());//不建議,不推薦的方法==此方法未來會不被支持
            System.exit(0);
            //只靠關閉activity是不能徹底退出的,這裏只是釋放了activity,還有其餘未釋放的資源經過重啓安裝包後調用System.exit(0);才能徹底退出。
        } catch (Exception e) {    }
    }java

 

 <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />android

http://developer.android.com/reference/android/app/ActivityManager.html#killBackgroundProcesses%28java.lang.String%29api

public void killBackgroundProcesses (String packageName)

Since: API Level 8

Have the system immediately kill all background processes associated with the given package. This is the same as the kernel killing those processes to reclaim memory; the system will take care of restarting these processes in the future as needed. app

You must hold the permission KILL_BACKGROUND_PROCESSES to be able to call this method.ide

Parameters
packageName The name of the package whose processes are to be killed.
相關文章
相關標籤/搜索