Android系统信息(内存、cpu、sd卡、电量、版本)获取

Android系统信息(内存、cpu、sd卡、电量、版本)获取

/*APPInfo.java*/
public class AppInfo {
    private String appLable;
    private Drawable appicon;
    private String pkgName;
    private String versionCode;
    private String versionName;
    private int service;
    private String num;
    private boolean isCheck;
    private int pid;
    
    public AppInfo(String appLable, Drawable appicon, String pkgName,
            String versionCode, String versionName, int service, String num,
            boolean isCheck, int pid) {
        super();
        this.appLable = appLable;
        this.appicon = appicon;
        this.pkgName = pkgName;
        this.versionCode = versionCode;
        this.versionName = versionName;
        this.service = service;
        this.num = num;
        this.isCheck = isCheck;
        this.pid = pid;
    }
    
    public AppInfo() {
        super();
    }
    
    public String getAppLable() {
        return appLable;
    }
    public void setAppLable(String appLable) {
        this.appLable = appLable;
    }
    
    public Drawable getAppicon() {
        return appicon;
    }
    public void setAppicon(Drawable appicon) {
        this.appicon = appicon;
    }
    
    public String getPkgName() {
        return pkgName;
    }
    public void setPkgName(String pkgName) {
        this.pkgName = pkgName;
    }
    
    public String getVersionCode() {
        return versionCode;
    }
    public void setVersionCode(String versionCode) {
        this.versionCode = versionCode;
    }
    
    public String getVersionName() {
        return versionName;
    }
    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }
    
    public int getService() {
        return service;
    }
    public void setService(int service) {
        this.service = service;
    }
    
    public String getNum() {
        return num;
    }
    public void setNum(String num) {
        this.num = num;
    }
    
    public boolean isCheck() {
        return isCheck;
    }
    public void setCheck(boolean isCheck) {
        this.isCheck = isCheck;
    }
    
    public int getPid() {
        return pid;
    }
    public void setPid(int pid) {
        this.pid = pid;
    }
}
 
/* AppInfoManager.java */
public class AppInfoManager {
    ArrayList<AppInfo> allapp;
    List<RunningAppProcessInfo> runningAppProcessInfos;
    private Context context;
    private ActivityManager mActivityManager;
    public AppInfoManager(Context context) {
        this.context=context;
        mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    }
    
    //全部应用
    public ArrayList<AppInfo> getAllApp(){
        ArrayList<AppInfo> allapp = new ArrayList<AppInfo>();
        PackageManager packageManager=context.getPackageManager();
        List<PackageInfo> packages=packageManager.getInstalledPackages(0);
        for(int i=0; i<packages.size(); i++){
            PackageInfo packageInfo=packages.get(i);
            AppInfo appInfo=new AppInfo();
            appInfo.setAppLable((String) packageInfo.applicationInfo.loadLabel(packageManager));
            appInfo.setPkgName(packageInfo.packageName);
            appInfo.setVersionName(packageInfo.versionName);
            appInfo.setVersionCode(packageInfo.versionCode+"");
            appInfo.setAppicon(packageInfo.applicationInfo.loadIcon(packageManager));
            allapp.add(appInfo);
        }
        return allapp;
    }
    
    //获取系统手机应用信息
    public List<AppInfo> getSystemApp(){
        ArrayList<AppInfo> appList = new ArrayList<AppInfo>();
        PackageManager packageManager=context.getPackageManager();
        List<PackageInfo> packages=packageManager.getInstalledPackages(0);
        for(int i=0; i<packages.size(); i++){
            PackageInfo packageInfo=packages.get(i);
            AppInfo appInfo=new AppInfo();
            appInfo.setAppLable((String) packageInfo.applicationInfo.loadLabel(packageManager));
            appInfo.setPkgName(packageInfo.packageName);
//            appInfo.setService(packageInfo.services.length);
            appInfo.setVersionName(packageInfo.versionName);
            appInfo.setVersionCode(packageInfo.versionCode+"");
            appInfo.setAppicon(packageInfo.applicationInfo.loadIcon(packageManager));
            
            if((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0){
                appList.add(appInfo);
            }
        }
        return appList;
    }
    
    //获得第三方应用
    public ArrayList<AppInfo> getThirsApp(){
        ArrayList<AppInfo> appList = new ArrayList<AppInfo>();
        PackageManager packageManager=context.getPackageManager();
        List<PackageInfo> packages=packageManager.getInstalledPackages(0);
        for(int i=0;i<packages.size();i++){
            PackageInfo packageInfo=packages.get(i);
            AppInfo appInfo=new AppInfo();
            appInfo.setAppLable((String) packageInfo.applicationInfo.loadLabel(packageManager));
            appInfo.setPkgName(packageInfo.packageName);
            appInfo.setVersionName(packageInfo.versionName);
            appInfo.setVersionCode(packageInfo.versionCode+"");
            appInfo.setAppicon(packageInfo.applicationInfo.loadIcon(packageManager));
            if((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0){
                appList.add(appInfo);
            }
        }
        return appList;
    }
        
    //获取手机占用内存
    public String getAvailMemory() {// 获取android当前可用内存大小  
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);  
        MemoryInfo mi = new MemoryInfo();  
        am.getMemoryInfo(mi);  
        //mi.availMem; 当前系统的可用内存  
        return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化  
    }  

    //获取手机全部内存
    public String getTotalMemory() {  
        String str1 = "/proc/meminfo";// 系统内存信息文件  
        String str2;  
        String[] arrayOfString;  
        long initial_memory = 0;  
  
        try {  
            FileReader localFileReader = new FileReader(str1);  
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);  
            str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小  
  
            arrayOfString = str2.split("\\s+");  
            for (String num : arrayOfString) {  
                Log.i(str2, num + "/t");  
            }  
          
            initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte  
            localBufferedReader.close();
        } catch (IOException e) {  
        }  
        return Formatter.formatFileSize(context, initial_memory);// Byte转换为KB或者MB,内存大小规格化  
    }  
    
    //获取手机正在运行的软件信息
    public ArrayList<AppInfo> getRunningAppInfo(){
        ArrayList<AppInfo> thirdAppNameList = new ArrayList<AppInfo>();  
        ArrayList<AppInfo> nowRunningApp = new ArrayList<AppInfo>();  
        thirdAppNameList = getThirsApp();  //用户程序
        runningAppProcessInfos = mActivityManager.getRunningAppProcesses();//获取正在运行的程序
        for (int i = 0; i < thirdAppNameList.size(); i++) {
            for (int j = 0; j < runningAppProcessInfos.size(); j++) {
                if(thirdAppNameList.get(i).getPkgName().equals(runningAppProcessInfos.get(j).processName)){
                    
                    int pid= runningAppProcessInfos.get(j).pid;  //包名匹配

                    //内存信息
                    Debug.MemoryInfo[] memoryInfos = mActivityManager.getProcessMemoryInfo(new int[]{pid});
                    String p = Formatter.formatFileSize(context, memoryInfos[0].dalvikPrivateDirty*1024);
                    
                    // 获得系统总内存,单位是KB,乘以1024转换为Byte,然后规格化
                    AppInfo app = new AppInfo();
                    app.setAppLable(thirdAppNameList.get(i).getAppLable());
                    app.setAppicon(thirdAppNameList.get(i).getAppicon());
                    app.setService(thirdAppNameList.get(i).getService());
                    app.setPid(pid);
                    app.setPkgName(thirdAppNameList.get(i).getPkgName());
                    app.setNum(p);
                    nowRunningApp.add(app);
                }
            }
        }
        return nowRunningApp;
    }  
}

 

/* PhoneManager.java */
public class PhoneManager {
    private Context context;
    public PhoneManager(Context context ){
        this.context=context;
    }
    
    //是否root
    public boolean isRoot(){
        boolean bool = false;
        try{
            if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists())){
                bool = false;
            } else {
                bool = true;
            }
        } catch (Exception e) {

        } 
        return bool;
    }
    
    //cpu参数
    public static String getMaxCpuFreq() {
        String result = "";
        ProcessBuilder cmd;
        try {
            String[] args = { "/system/bin/cat",
            "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
            cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            InputStream in = process.getInputStream();
            byte[] re = new byte[24];
            while (in.read(re) != -1) {
                result = result + new String(re);
            }
            if(!"".equals(result.trim())){
                result=""+(Integer.parseInt(result.trim())/1000);
            }
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            result = "N/A";
        }
        return result ;
    }

    // 获取CPU最小频率(单位KHZ)
    public static String getMinCpuFreq() {
        String result = "";
        ProcessBuilder cmd;
        try {
            String[] args = { "/system/bin/cat",
            "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };
            cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            InputStream in = process.getInputStream();
            byte[] re = new byte[24];
            while (in.read(re) != -1) {
                result = result + new String(re);
            }
            in.close();
            if(!"".equals(result.trim())){
                result=""+(Integer.parseInt(result.trim())/1000);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            result = "N/A";
        }
        return result.trim();
    }

    // 实时获取CPU当前频率(单位KHZ)
    public static String getCurCpuFreq() {
        String result = "N/A";
        try {
            FileReader fr = new FileReader(
                    "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
            BufferedReader br = new BufferedReader(fr);
            String text = br.readLine();
            result = text.trim();
            if(!"N/A".equals(result.trim())){
                result=""+(Integer.parseInt(result.trim())/1000);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    // 获取CPU名字
    public static String getCpuName() {
        try {
            FileReader fr = new FileReader("/proc/cpuinfo");
            BufferedReader br = new BufferedReader(fr);
            String text = br.readLine();
            String[] array = text.split(":\\s+", 2);
            for (int i = 0; i < array.length; i++) {
            }
            return array[1];
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    // 获取CPU核数
    public static int getNumCores() { 
        //Private Class to display only CPU devices in the directory listing 
        class CpuFilter implements FileFilter { 
        @Override 
        public boolean accept(File pathname) { 
            //Check if filename is "cpu", followed by a single digit number 
            if(Pattern.matches("cpu[0-9]", pathname.getName())) { 
                return true; 
            } 
                return false; 
            } 
        } 
        try { 
        //Get directory containing CPU info 
        File dir = new File("/sys/devices/system/cpu/"); 
        //Filter to only list the devices we care about 
        File[] files = dir.listFiles(new CpuFilter()); 
            //Return the number of cores (virtual CPU devices) 
            return files.length; 
        } catch(Exception e) { 
            //Default to return 1 core 
            return 1; 
        } 
    }
    
    //SD卡大小
    public String getSDTotalSize() {  
        File path = Environment.getExternalStorageDirectory();  
        StatFs stat = new StatFs(path.getPath());  
        long blockSize = stat.getBlockSize();  
        long totalBlocks = stat.getBlockCount();  
        return Formatter.formatFileSize(context, blockSize * totalBlocks);  
    }  
    
    // 判断设备是否支持多点触控  
    public static boolean isSupportMultiTouch(Context context) {  
        PackageManager pm = context.getPackageManager();  
        boolean isSupportMultiTouch = pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);  
        return isSupportMultiTouch;  
    } 
    
    //手机分辨率
    public String getWindow(){
        WindowManager wm=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int height=wm.getDefaultDisplay().getHeight();
        int width = wm.getDefaultDisplay().getWidth();
        return height+"*"+width;
    }
    
    //手机像素密度
    public static String  getDen(){
        DisplayMetrics metric = new DisplayMetrics();
        float density = metric.density;
        return density+"";
    }
    
    // .获取手机MAC地址 , 只有手机开启wifi才能获取到mac地址 
    public String getMacAddress(){  
       String result = "";  
       WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);  
       WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
       result = wifiInfo.getMacAddress();  
       Log.i("text", "手机macAdd:" + result);  
       return result;  
    }  

    //得到网络类型
    public String getNet(){
        String net=null;
        TelephonyManager mTm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
        switch (mTm.getNetworkType()) {
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            net="未知网络";
            break;
        case TelephonyManager.NETWORK_TYPE_GPRS:
            net="GPRS网络"; 
            break;
        case TelephonyManager.NETWORK_TYPE_EDGE:
            net="EDGE网络"; 
            break;
        case TelephonyManager. NETWORK_TYPE_UMTS :
            net="UMTS网络";
            break;
        case TelephonyManager.NETWORK_TYPE_HSUPA :
            net="HSUPA网络";
            break;
        case TelephonyManager.NETWORK_TYPE_HSPA:
            net="HSPA网络";
            break;
        case TelephonyManager.NETWORK_TYPE_CDMA :
            net="CDMA网络";
            break;
        case TelephonyManager. NETWORK_TYPE_EVDO_0:
            net="EVDO0网络";
            break;
        case TelephonyManager.NETWORK_TYPE_HSDPA:
            net="HSDPA网络";
            break;
        }
        return net;
    }
}

 

参考链接:http://gqdy365.iteye.com/blog/1066113

你可能感兴趣的:(android)