Android实现仿360桌面悬浮清理内存
作者:hebedich 时间:2021-08-25 11:21:05
今天闲来无事写了一个清内存的小东西,类似360,在桌面上悬浮,点击后清除后台无用程序,清除后台程序是通过调用ActivityManger.killBackgroundProcesses的方式来进行的,这样的方式有个不好的地方,就是重要性级别设置的高的应用杀不掉。关键代码如下所示
ActivityManager mActivityManager = MyManager.getActivityManager(mContext);
List<ActivityManager.RunningAppProcessInfo> process = mActivityManager.getRunningAppProcesses();
for(int i=0;i<process.size();i++){
ActivityManager.RunningAppProcessInfo ar = process.get(i);
String packageName = ar.processName;
packageName = packageName.split(":")[0];
//重要级别大于200,并且不被信任的后台将被杀掉
if(ar.importance>100 && !MyManager.isTrust(packageName)){
MyManager.getActivityManager(mContext).killBackgroundProcesses(packageName);
}
}
还有一个部分功能是,桌面悬浮,这个功能是通过WindowManger的对象添加到桌面上的,关键代码如下所示
WindowManager windowManager = getWindowManager(context);
int screenWidth = windowManager.getDefaultDisplay().getWidth();
int screenHeight = windowManager.getDefaultDisplay().getHeight();
if(mSmallFloatWin==null){
mSmallFloatWin = new SmallFloatWin(context);
if (smallWindowParams == null) {
smallWindowParams = new LayoutParams();
smallWindowParams.type = LayoutParams.TYPE_PHONE;
smallWindowParams.format = PixelFormat.RGBA_8888;
smallWindowParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
smallWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
smallWindowParams.width = SmallFloatWin.viewWidth;
smallWindowParams.height = SmallFloatWin.viewHeight;
smallWindowParams.x = screenWidth;
smallWindowParams.y = screenHeight / 2;
}
mSmallFloatWin.setParams(smallWindowParams);
windowManager.addView(mSmallFloatWin, smallWindowParams);
}
其中mSmallFloatWin就是要悬浮出来的视图对象了。
后记:这个小东西杀后台程序效果不算好,还有一种思路通过root的权限去执行adb命令来进行后台程序。
附上源码 点击下载
标签:Android,悬浮,清理内存
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android屏幕旋转之横屏竖屏切换的实现
2023-10-29 21:40:10
SpringMVC中的handlerMappings对象用法
2023-01-09 21:38:36
![](https://img.aspxhome.com/file/2023/5/75015_0s.png)
Java结构型模式之门面模式详解
2023-07-29 10:26:55
![](https://img.aspxhome.com/file/2023/8/91058_0s.png)
Bootstrap 下拉菜单.dropdown的具体使用方法
2023-07-08 19:10:46
maven中配置项目的jdk版本无效的排查方式
2023-07-18 21:43:42
![](https://img.aspxhome.com/file/2023/9/57509_0s.png)
Java基于二叉查找树实现排序功能示例
2022-09-04 21:50:52
Java中的动态和静态编译实例详解
2021-07-30 15:25:10
Android HorizontalScrollView滑动与ViewPager切换案例详解
2023-06-05 00:48:27
基于java math API 的详细解释说明
2021-10-04 06:51:44
java设计模式笔记之适配器模式
2021-12-27 05:27:30
![](https://img.aspxhome.com/file/2023/2/62242_0s.png)
浅谈java 单例模式DCL的缺陷及单例的正确写法
2022-04-14 05:58:23
![](https://img.aspxhome.com/file/2023/5/76615_0s.jpg)
提高C# StringBuilder操作性能优化的方法
2023-04-26 01:16:13
![](https://img.aspxhome.com/file/2023/3/96323_0s.jpg)
SpringBoot统一响应格式及统一异常处理
2022-08-30 08:03:08
![](https://img.aspxhome.com/file/2023/5/63155_0s.png)
详解SpringBoot配置文件启动时动态配置参数方法
2022-03-05 03:20:02
![](https://img.aspxhome.com/file/2023/0/80280_0s.jpg)
解决RestTemplate 请求接收自定义400+ 或500+错误
2023-12-12 00:25:20
![](https://img.aspxhome.com/file/2023/6/85426_0s.png)
Java中线程状态+线程安全问题+synchronized的用法详解
2023-08-23 08:38:07
![](https://img.aspxhome.com/file/2023/6/78856_0s.png)
java中public class与class的区别详解
2023-04-09 11:49:40
Java实现word/pdf转html并在线预览
2022-09-09 09:16:49
![](https://img.aspxhome.com/file/2023/5/65635_0s.png)
Java Thread 类和Runnable 接口详解
2023-11-10 20:16:00
![](https://img.aspxhome.com/file/2023/7/59017_0s.png)
java实习--每天打卡十道面试题!
2021-12-06 15:22:19
![](https://img.aspxhome.com/file/2023/2/85522_0s.png)