Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.app.ActivityManager;

import android.content.Context;
import android.os.Build;

import java.util.List;

public class Main {

    public static void releaseMemory(Context context) {
        ActivityManager activityManger = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> list = activityManger.getRunningAppProcesses();
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                ActivityManager.RunningAppProcessInfo apinfo = list.get(i);
                String[] pkgList = apinfo.pkgList;
                if (apinfo.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE
                        && !apinfo.processName.equals("com.tt.realeasememory")) {
                    for (int j = 0; j < pkgList.length; j++) {
                        if (Build.VERSION.SDK_INT >= 8) {
                            activityManger.killBackgroundProcesses(pkgList[j]);
                        } else {
                            activityManger.restartPackage(pkgList[j]);
                        }
                    }
                }
            }
        }
    }
}