Android implements a memory cleanup mimicking 360 desktop levitation

  • 2020-12-09 01:00:26
  • OfStack

Today, be free and at leisure wrote a small thing to clear the memory, similar to 360, hovering on the desktop, click to clear the background useless programs, the background program is cleared by calling ActivityManger.killBackgroundProcesses way, this way has a bad place, is the importance level set high application can not kill. The key code is shown below


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];
     // Importance level is greater than 200 And untrusted backers will be killed 
     if(ar.importance>100 && !MyManager.isTrust(packageName)){
      MyManager.getActivityManager(mContext).killBackgroundProcesses(packageName);
     }
    }

Another part of the feature is desktop levitation, which is added to the desktop via WindowManger objects, and the key code is shown below


 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 is the view object you want to float out of.

Postscript: this little thing kill background program effect is not good, there is another way of thinking through root permission to execute adb command to carry out background program.

Attached source click download


Related articles: