Android结束进程方法
上篇随笔说到最近在做应用切换图标的工作,需要在设置的显示里添加一个选项用来对图标进行切换,这里遇到一个问题,切换图标的具体实现是在底层,调用是在launcher的IconCache.java类中,可是这里只会在启动时调用一次,在后面我想进行图标切换的时候就必须重启launcher应用,其实好多对系统的改动都需要做重启应用操作,这时我们可以用以下方法:
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.
packageName |
The name of the package to be stopped. |
使用这个类的具体源代码
Java代码
final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage(PackageName);
再在执行重启操作的应用的AndroidManifest.xml加上以下权限即可。
Xml代码
<uses-permission android:name="android.permission.RESTART_PACKAGES"></uses-permission>
这里的应用必须具有系统级的权限,如settings应用。AndroidManifest.xml中必须有android:sharedUserId="android.uid.system"
其他结束进程方法可参考http://blog.csdn.net/Zengyangtech/article/details/5733631,感谢博主