zoukankan      html  css  js  c++  java
  • Android桌面悬浮清内存app概述

      今天闲来无事写了一个清内存的小东西,类似360,在桌面上悬浮,点击后清除后台无用程序,清除后台程序是通过调用ActivityManger.killBackgroundProcesses的方式来进行的,这样的方式有个不好的地方,就是重要性级别设置的高的应用杀不掉。关键代码如下所示

      

     1 ActivityManager mActivityManager = MyManager.getActivityManager(mContext);
     2                 List<ActivityManager.RunningAppProcessInfo> process = mActivityManager.getRunningAppProcesses();
     3 
     4                 for(int i=0;i<process.size();i++){
     5                     ActivityManager.RunningAppProcessInfo ar = process.get(i);
     6                     String packageName = ar.processName;
     7                     packageName = packageName.split(":")[0];
     8                     //重要级别大于200,并且不被信任的后台将被杀掉
     9                     if(ar.importance>100 && !MyManager.isTrust(packageName)){
    10                         MyManager.getActivityManager(mContext).killBackgroundProcesses(packageName);
    11                     }
    12                 }

      还有一个部分功能是,桌面悬浮,这个功能是通过WindowManger的对象添加到桌面上的,关键代码如下所示

      

     1 WindowManager windowManager = getWindowManager(context); 
     2         int screenWidth = windowManager.getDefaultDisplay().getWidth();
     3         int screenHeight = windowManager.getDefaultDisplay().getHeight();
     4 
     5         if(mSmallFloatWin==null){
     6             mSmallFloatWin = new SmallFloatWin(context);
     7             if (smallWindowParams == null) {  
     8                 smallWindowParams = new LayoutParams();  
     9                 smallWindowParams.type = LayoutParams.TYPE_PHONE;  
    10                 smallWindowParams.format = PixelFormat.RGBA_8888;  
    11                 smallWindowParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL  
    12                         | LayoutParams.FLAG_NOT_FOCUSABLE;  
    13                 smallWindowParams.gravity = Gravity.LEFT | Gravity.TOP;  
    14                 smallWindowParams.width = SmallFloatWin.viewWidth;  
    15                 smallWindowParams.height = SmallFloatWin.viewHeight;  
    16                 smallWindowParams.x = screenWidth;  
    17                 smallWindowParams.y = screenHeight / 2;  
    18             }  
    19             mSmallFloatWin.setParams(smallWindowParams);
    20             windowManager.addView(mSmallFloatWin, smallWindowParams);  
    21         }

      其中mSmallFloatWin就是要悬浮出来的视图对象了。

      后记:这个小东西杀后台程序效果不算好,还有一种思路通过root的权限去执行adb命令来进行后台程序。

      完整Demo下载路径:MyFloat

  • 相关阅读:
    973. K Closest Points to Origin
    919. Complete Binary Tree Inserter
    993. Cousins in Binary Tree
    20. Valid Parentheses
    141. Linked List Cycle
    912. Sort an Array
    各种排序方法总结
    509. Fibonacci Number
    374. Guess Number Higher or Lower
    238. Product of Array Except Self java solutions
  • 原文地址:https://www.cnblogs.com/luoaz/p/3715222.html
Copyright © 2011-2022 走看看