clear Memory - Android Hardware

Android examples for Hardware:Memory

Description

clear Memory

Demo Code


//package com.java2s;

import java.util.List;
import android.app.ActivityManager;
import android.content.Context;

public class Main {

    public static void clearMemory(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) {
                    for (int j = 0; j < pkgList.length; j++) {
                        try {
                            if (!apinfo.processName.contains("your name")) {
                                activityManger
                                        .killBackgroundProcesses(pkgList[j]);
                            }/*from w ww .  j a v a2  s . c o  m*/
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

Related Tutorials