如何正常关闭android应用程序
2012-10-11 15:49
357 查看
1.finish();
2.android.os.Process.killProcess(android.os.Process.myPid());
3.Android彻底关闭当前应用
以下方法用于关闭当前应用:
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
manager.restartPackage(getPackageName());
需要权限:
"Android.permission.RESTART_PACKAGES"
函数说明:
void Android.app.ActivityManager.restartPackage(String packageName)
public void restartPackage (String packageName)
Since: API Level 3
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 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 RESTART_PACKAGES to be able to call this method.
Parameters
packageName The name of the package to be stopped.
与当前应用相关的应用、进程、服务等也会被关闭。
会发送 ACTION_PACKAGE_RESTARTED广播。
不要被函数名误导。
ps:要通过一个进程去结束另一个进程。在之前的SDK版本中,一直使用方法restartPackage(packageName)方法,但是在Android的开发文档中介绍说,这个函数会导致一些问题( 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.),所以建议大家使用一个新的方法:
void killBackgroundProcesses(String packageName)
由于这个方法没有返回值,所以我们也不知道我们的目标进程是否真的退出了。但是,我目前只发现了这个可以结束另一个进程的方法。
2.android.os.Process.killProcess(android.os.Process.myPid());
3.Android彻底关闭当前应用
以下方法用于关闭当前应用:
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
manager.restartPackage(getPackageName());
需要权限:
"Android.permission.RESTART_PACKAGES"
函数说明:
void Android.app.ActivityManager.restartPackage(String packageName)
public void restartPackage (String packageName)
Since: API Level 3
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 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 RESTART_PACKAGES to be able to call this method.
Parameters
packageName The name of the package to be stopped.
与当前应用相关的应用、进程、服务等也会被关闭。
会发送 ACTION_PACKAGE_RESTARTED广播。
不要被函数名误导。
ps:要通过一个进程去结束另一个进程。在之前的SDK版本中,一直使用方法restartPackage(packageName)方法,但是在Android的开发文档中介绍说,这个函数会导致一些问题( 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.),所以建议大家使用一个新的方法:
void killBackgroundProcesses(String packageName)
由于这个方法没有返回值,所以我们也不知道我们的目标进程是否真的退出了。但是,我目前只发现了这个可以结束另一个进程的方法。
相关文章推荐
- Android 教你如何关闭应用程序
- Android 应用程序(APK) 如何获得系统签名权限 强制关闭程序(后台进程)
- Android开发:如何按back键 彻底的关闭一个应用程序(调用finish()不太奏效)
- Android开发:如何按back键 彻底的关闭一个应用程序(调用finish()不太奏效)
- Android 应用程序(APK) 如何获得系统签名权限 强制关闭程序(后台进程)
- Android程序有很多Activity,在B中如何关闭整个Android应用程序,关闭所有的 Activity
- 如何正常关闭android应用
- Android-正常关闭应用程序
- Android如何在退出App时关闭整个应用程序
- 如何发布android应用程序
- 如何发布你的Android应用程序
- 如何在真机上调试Android应用程序(图文详解)
- 如何使Android应用程序获取系统权限【转】
- Android中如何修改系统时间(应用程序获得系统权限)
- Android中应用程序如何获得系统签名权限
- 如何让android应用程序用中文显示(应用程序名称本地化)
- 如何正常关闭一个套接字
- Android开发教程——应用程序之间如何实现沟通
- android如何定制默认桌面上应用程序和shortcut图标
- android如何从应用程序进入设置的各个页面