在非ROOT的情况下,强制关闭其他程序,使自己的程序总是处于前台,可以把关闭其他程序或是进程的方法放在BaseActivity的onStop方法中,同时也可以放在一个定时方法中。强制关闭其他程序基本都用的 killBackgroundProcesses 或是 forceStopPackage 。其中 killBackgroundProcesses 使用起来更简单一些,不需要系统签名,但是缺点是不能关闭其他前台程序,仅对后台有效,并且系统会在需要的时候再次重启被我们杀死的进程,所以如果对关闭前台程序这一点没有特别需求的话,可以使用 killBackgroundProcesses 。forceStopPackage方法可以关闭前、后台程序,但是该方法已经隐藏,需要使用反射的方法并配合系统签名来使用。下面来说下具体用法。
目录
一)void killBackgroundProcesses(String packageName)
二)void forceStopPackage(String packageName)
一)void killBackgroundProcesses(String packageName)
方法:
/**
* 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.
*
* @param packageName The name of the package whose processes are to
* be killed.
*/
@RequiresPermission(Manifest.permission.KILL_BACKGROUND_PROCESSES)
public void killBackgroundProcesses(String packageName) {
try {
getService().killBackgroundProcesses(packageName,
mContext.getUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
注意注释中这句话: the system will take care of restarting these processes in the future as needed表明系统会在需要的时候再次重启被我们杀死的进程。同时需要的权限也注明了为:Manifest.permission.KILL_BACKGROUND_PROCESSES 。
使用方法:
- 首先别忘记了在AndroidManifest.xml文件中添加权限(KILL_BACKGROUND_PROCESSES):
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
- 主要代码
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE) ;
manager.killBackgroundProcesses(packageName) ;
/**
* 查询当前进程列表 (含系统应用)
*/
private void queryFilterAppInfo() {
PackageManager pm = this.getPackageManager();
List<ApplicationInfo> appInfos= pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
List<ApplicationInfo> applicationInfos=new ArrayList<>();
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveinfoList = getPackageManager()
.queryIntentActivities(resolveIntent, 0);
Set<String> allowPackages=new HashSet();
for (ResolveInfo resolveInfo:resolveinfoList){
allowPackages.add(resolveInfo.activityInfo.packageName);
}
for (ApplicationInfo app:appInfos) {
String name = app.processName;
// 这里是你需要保留的程序或进程的包名 com.xxx.xxx
if(name.equals("com.xxx.xxx")||
name.equals("com.xxxx.xxxx")) continue;
if((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0)
{
applicationInfos.add(app);
}
if(app.uid > 10000){
applicationInfos.add(app);
}
if (allowPackages.contains(app.packageName)){
applicationInfos.add(app);
}
}
for (ApplicationInfo applicationInfo : applicationInfos) {
System.out.println(applicationInfo.packageName);
doExec(applicationInfo.packageName);
}
}
/**
* 结束程序,如果只是想单纯的结束某个应用,也可以只用这段代码,传入包名就好
*/
private void doExec(String packageName) {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE) ;
manager.killBackgroundProcesses(packageName) ;
}
二)void forceStopPackage(String packageName)
2.1 forceStopPackage使用
方法:
/**
* 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.
*
* <p>You must hold the permission
* {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to
* call this method.
*
* @param packageName The name of the package to be stopped.
* @param userId The user for which the running package is to be stopped.
*
* @hide This is not available to third party applications due to
* it allowing them to break other applications by stopping their
* services, removing their alarms, etc.
*/
@UnsupportedAppUsage
public void forceStopPackageAsUser(String packageName, int userId) {
try {
getService().forceStopPackage(packageName, userId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @see #forceStopPackageAsUser(String, int)
* @hide
*/
@SystemApi @TestApi
@RequiresPermission(Manifest.permission.FORCE_STOP_PACKAGES)
public void forceStopPackage(String packageName) {
forceStopPackageAsUser(packageName, mContext.getUserId());
}
使用方法:
- 首先别忘记了在AndroidManifest.xml文件中添加权限(FORCE_STOP_PACKAGES):
<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"/>
- 因为该方法需要配合platform签名一起使用,所以需要在application中加入系统级权限来共享系统进程属性:android:sharedUserId="android.uid.system"
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:sharedUserId="android.uid.system"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
- 然后使用反射机制调用方法
private void stopAppByForce(Context context , String packageName) {
ActivityManager mActivityManager = (ActivityManager)
context.getSystemService(Context.ACTIVITY_SERVICE);
Method method = null;
try {
method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
method.invoke(mActivityManager, packageName);
} catch (Exception e) {
e.printStackTrace();
}
}
可以搭配下面代码一起使用
/**
* 查询当前进程列表 (含系统应用)
*/
private void queryFilterAppInfo() {
PackageManager pm = this.getPackageManager();
List<ApplicationInfo> appInfos= pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
List<ApplicationInfo> applicationInfos=new ArrayList<>();
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveinfoList = getPackageManager()
.queryIntentActivities(resolveIntent, 0);
Set<String> allowPackages=new HashSet();
for (ResolveInfo resolveInfo:resolveinfoList){
allowPackages.add(resolveInfo.activityInfo.packageName);
}
for (ApplicationInfo app:appInfos) {
String name = app.processName;
// 这里是你需要保留的程序或进程的包名 com.xxx.xxx
if(name.equals("com.xxx.xxx")||
name.equals("com.xxxx.xxxx")) continue;
if((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0)
{
applicationInfos.add(app);
}
if(app.uid > 10000){
applicationInfos.add(app);
}
if (allowPackages.contains(app.packageName)){
applicationInfos.add(app);
}
}
for (ApplicationInfo applicationInfo : applicationInfos) {
System.out.println(applicationInfo.packageName);
stopAppByForce(this, applicationInfo.packageName);
}
}
注:
这个时候如果直接使用,会报错:java.lang.SecurityException: Permission Denial: forceStopPackage() from pid=2435, uid=10066 requires android.permission.FORCE_STOP_PACKAGES
原因:android.permission.FORCE_STOP_PACKAGES权限是在frameworks/base/core/res/AndroidManifest.xml文件中声明,如下:
<permission android:name="android.permission.FORCE_STOP_PACKAGES"
android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
android:protectionLevel="signature"
android:label="@string/permlab_forceStopPackages"
android:description="@string/permdesc_forceStopPackages"/>
注意protectionLevel属性值未signature。看sdk文档http://developer.android.com/guide/topics/manifest/permission-element.html#plevel中对这一属性的解释如下:
意思是:app使用FORCE_STOP_PACKAGES权限,app必须和这个权限的声明者的签名保持一致。
FORCE_STOP_PACKAGES的声明者是frameworks/base/core/res/,可以在frameworks/base/core/res/Android.mk中看到它的签名信息:
LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_PACKAGE_NAME := framework-res
LOCAL_CERTIFICATE := platform
即,签名为platform.
最终得到结论,app需要是platform签名,才可以使用forceStopPackage方法。
2.2 配置系统签名
2.2.1 准备工作
- 在github上下载工具
keytool-importkeypair点击下载 - Android证书文件准备
文件路径:Android/build/target/product/security,包括platform.pk8和platform.x509.pem
模拟器的可以去http://androidxref.com/下载对应版本的证书文件。
(也可以偷懒一起下载我这里下载好的文件-我下载的是5.1版本的:链接:https://pan.baidu.com/s/1sKA6pTQb3Hwm1BJLf_PTsw
提取码:v4p8
或是这里:https://download.csdn.net/download/qq_20523943/12918526 )
2.2.2 步骤
1.生成keystore文件
- 将keytool-importkeypair、platform.pk8和platform.x509.pem文件放在同一个目录下,执行如下命令,会生成platform.keystore文件:
比如我都放在D盘下的 platform签名 文件夹中sh keytool-importkeypair -k ./platform.keystore -p android -pk8 platform.pk8 -cert platform.x509.pem -alias platform
执行命令 -
-p 表示新生成的keystore的密码是什么,这里为android
-
-pk8 表示要导入的pk8文件的名称,可以包括路径,pk8文件用来保存private key的,是个私钥文件。
-
-cert 表示要导入的证书文件,和pk8文件在同一个目录,pem这种文件就是一个X.509的数字证书,里面有用户的公钥等信息,是用来解密的,这种文 件格式里面不仅可以存储数字证书,还能存各种key。
-
-alias 表示给生成的platform.keystore取一个别名,这个名字只有我们在签名的时候才用的到,这里我们生成的文件名是platform。这个名字,可以随便取,但是你自己一定要记住。
2.AndroidStudio添加keystore
- 添加keystore,打包apk
- 选择第二个选项,APK
- 两个密码和alias就是我们之前用keytool-importkeypair命令生成的,我这边使用的比较简单的密码“android”,alias为platform
-
选择打包的位置和选择版本,是debug还是release版本,我测试的时候选择的是debug,下面的两个选项也要打勾,最后就打包成apk了。
-
如果想要在run app的时候系统签名也可以用的话,还需要做一些措施,设置run app的时候的系统签名
-
添加keystore文件
至此基本就差不多了,下面是最终效果,其中点击第一个程序中间的文字5秒后会执行关闭其他程序的方法,第二个程序是我通过adb启动的一个要关闭的程序
参考:
https://blog.csdn.net/huxueyan521/article/details/8921976
https://www.jianshu.com/p/381f3b854097