您的位置:首页 > 移动开发

OSC android app 退出方法改进

2012-10-13 00:00 239 查看
/**
* 应用程序Activity管理类:用于Activity管理和应用程序退出
* @author liux (http://my.oschina.net/liux)
* @version 1.0
* @created 2012-3-21
*/
public class AppManager {
.......

/**
* 退出应用程序
*/
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) { }
}

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
http://developer.android.com/reference/android/app/ActivityManager.html#killBackgroundProcesses%28java.lang.String%29

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.

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

Parameters
packageName The name of the package whose processes are to be killed.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: