关闭

获取硬件信息

513人阅读 评论(0) 收藏 举报

 通过shell命令获取硬件信息,如硬盘使用,内存,cpu等

ShellProcessor pro = new ShellProcessor();
StringBuilder sbu = new StringBuilder();

final String[] diskCmd = {"/system/bin/df"};
String disk = pro.execute(diskCmd);
sbu.append("硬盘信息=========\n").append(disk).append("\n").append(parseDiskinfo(disk).toString()).append("\n");
						
final String[] cpuCmd = {"/system/bin/cat","/proc/cpuinfo"};
String cpu = pro.execute(cpuCmd);
sbu.append("CPU信息=========\n").append(cpu).append("\n");
			
final String[] memCmd = {"/system/bin/cat","/proc/meminfo"};
String memory = pro.execute(memCmd);
sbu.append("内存信息=========\n").append(memory).append("\n");
			
final String[] versionCmd = {"/system/bin/cat","/proc/version"};
String version = pro.execute(versionCmd);
sbu.append("OS版本===========\n").append(version).append("\n");
tv.setText(sbu.toString());

另外,在/proc/下还有其他很多有用的命令。 

 

ShellProcessor类:

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.util.Log;

/**
 * 执行Shell脚本
 * @author QiShuang
 *
 */
public final class ShellProcessor {
	
	private ShellProcessor(){}

	/**
	 * 执行Shell脚本
	 * @param cmd String...
	 * @return
	 */
	public static String execute(String  cmd){
		String result = null;
		Process process = null;
		try {
			ProcessBuilder builder = new ProcessBuilder(cmd);
			process = builder.start();
			//操作输入流
			InputStream in = process.getInputStream();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			FileHandler.FileRWHandle.transIOStream(in, out);
			result = new String(out.toByteArray());
			//输出错误信息(如果有)
			InputStream errIn = process.getErrorStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(errIn));
			String line = "";
			while ((line = br.readLine()) != null) {
				System.err.println("shell exe error: " + line);
			}
			process.waitFor();
			in.close();
			out.close();
			br.close();
		} catch (Exception e) {
			Log.e(ShellProcessor.class.getName(), "执行shell脚本出错:"+e.toString());
		} finally{
			if(process!=null)
				process.destroy();
		}
		return result;
	}
}






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22500次
    • 积分:378
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:10篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论