Android-如何让service 不会被第三方kill

本文介绍了一种防止Android服务被第三方安全任务管理软件误杀的方法。通过设置前台服务并使用特定的Notification标志,可以确保服务即使在清理缓存时也能保持运行状态。

最近写service的时候,用360等第三方安全任务管理软件清理缓存的时候总是会kil掉服务,于是找了解决方案

Notification notification = new Notification(R.drawable.icon, "服务开启", System.currentTimeMillis());
notification.flags|= Notification.FLAG_NO_CLEAR;  
notification.flags=Notification.FLAG_ONGOING_EVENT;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "service", "防止服务被任务管理器所杀", pendingIntent);     
startForeground(ONGOING_NOTIFICATION, notification);
这样就能显示一个前台服务,通知栏正在运行有一个显示,清除不了,如果不想显示通知栏又想服务不被杀死呢?

Notification notification = new Notification();
startForeground(1, notification);

这样就可以!谷歌上说其实在极端环境下,服务也是会被杀死的,但是这种情况很小,基本不是问题

It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.



### 3.1 通过包名关闭服务或应用的实现方式 在Android系统中,关闭特定应用或其服务可以通过系统API或反射机制实现。由于Android系统对应用权限的严格限制,直接关闭其他应用或其服务通常需要系统级权限或特殊调用方式。 #### 3.1.1 使用ActivityManager清理应用数据 通过`ActivityManager`的`clearApplicationUserData`方法,可以根据应用包名清理其用户数据,从而间接实现关闭应用的效果。此方法通常用于系统应用或具有系统权限的应用中。 ```java public void clearAppDatas(String pkg, Context context) { final long ident = Binder.clearCallingIdentity(); try { ActivityManager ams = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); boolean result = ams.clearApplicationUserData(pkg, null); } catch (Exception e) { e.printStackTrace(); } finally { Binder.restoreCallingIdentity(ident); } } ``` 该方法适用于系统服务中调用,并能够清除应用的用户数据[^3]。 #### 3.1.2 使用反射调用隐藏方法终止进程 Android系统中存在一些隐藏的API,例如`ActivityManager`中的`killBackgroundProcesses`方法,可以用来终止指定包名的后台进程。由于这些方法未公开,调用时需要使用Java反射机制,并且应用必须具备系统权限。 在`AndroidManifest.xml`中添加以下权限: ```xml <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> ``` 调用示例代码如下: ```java try { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); Method method = ActivityManager.class.getMethod("killBackgroundProcesses", String.class); method.invoke(activityManager, "com.example.targetapp"); } catch (Exception e) { e.printStackTrace(); } ``` 该方法适用于需要终止后台进程的场景,但需注意其依赖系统权限和反射机制[^2]。 #### 3.1.3 强制停止应用 通过系统设置中的应用管理器,可以强制停止指定应用。开发者也可以通过调用系统API实现类似功能。获取`PackageManager`实例后,可以调用`clearApplicationUserData`方法来清理应用数据并强制停止其运行。 ```java PackageManager packageManager = context.getPackageManager(); packageManager.clearApplicationUserData("com.example.targetapp", null); ``` 此方法会清除应用的用户数据并终止其运行,适用于系统级应用或具有特殊权限的应用[^1]。 #### 3.1.4 启动指定Activity并关闭应用 通过包名和目标`Activity`的类名,可以使用`Intent`启动指定应用的界面。如果希望在启动后关闭该应用,可以在启动后发送关闭指令或通过其他方式终止其运行。 ```java Intent intent = new Intent(); intent.setComponent(new ComponentName("com.example.targetapp", "com.example.targetapp.MainActivity")); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); ``` 该方法用于启动应用的主界面,若需关闭应用,需结合其他方式实现[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值