25、显示硬件信息(cpu信息、内存信息、硬盘信息、显示屏信息)

-----------------------main.java-------------------------


package com.example.hardware;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class MainActivity extends ActionBarActivity implements OnItemClickListener{


public static final int CPU_INFO = 1;
public static final int MEM_INFO = 2;
public static final int DISK_INFO = 3;
public static final int DisplayMetrics = 4;


ListView itemlist = null;
List> list;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("硬件信息");
itemlist = (ListView) findViewById(R.id.itemlist);
refreshListItems();
}


private void refreshListItems() {
list = buildListForSimpleAdapter();
SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.info_row,
new String[] { "name", "desc" }, new int[] { R.id.name,
R.id.desc });
itemlist.setAdapter(notes);
itemlist.setOnItemClickListener(this);
itemlist.setSelection(0);
}

private List> buildListForSimpleAdapter() {
List> list = new ArrayList>(3);
// Build a map for the attributes
Map map = new HashMap();

map = new HashMap();
map.put("id", MainActivity.CPU_INFO);
map.put("name", "CPU信息");
map.put("desc", "获取CPU信息");
list.add(map);



map = new HashMap();
map.put("id", MainActivity.MEM_INFO);
  map.put("name", "内存信息");
map.put("desc", "获取手机的内存信息.");
list.add(map);


map = new HashMap();
map.put("id", MainActivity.DISK_INFO);
  map.put("name", "硬盘信息");
map.put("desc", "获取硬盘信息.");
list.add(map);

 
map = new HashMap();
map.put("id", MainActivity.DisplayMetrics);
  map.put("name", "显示屏信息");
map.put("desc", "获取显示屏信息.");
list.add(map);

return list;
}


@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
Intent intent = new Intent();
Bundle info = new Bundle();
Map map = list.get(position);
info.putInt("id",  (Integer) map.get("id"));
info.putString("name", (String) map.get("name"));
info.putString("desc", (String) map.get("desc"));
intent.putExtra("android.intent.extra.info", info);
intent.setClass(MainActivity.this, ShowInfoActivity.class);
startActivityForResult(intent, 0);
}
}


-----------------------------ShowInfoActivity.java----------------------


package com.example.hardware;


 
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;


public class ShowInfoActivity extends Activity implements Runnable {


TextView info;
TextView title;
private ProgressDialog pd;
public String info_datas;
public boolean is_valid = false;
public int _id = 0;
public String _name = "";


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showinfo);
revParams();
info = (TextView) findViewById(R.id.info);
title = (TextView) findViewById(R.id.title);
setTitle("eoeInfosAssistant: " + _name);
title.setText(_name);
load_data();
}


private void load_data() {
pd = ProgressDialog.show(this, "Please Wait a moment..",
"fetch info datas...", true, false);
Thread thread = new Thread(this);
thread.start();
}


// 接收传递进来的信息
private void revParams() {
Intent startingIntent = getIntent();
if (startingIntent != null) {
Bundle infod = startingIntent
.getBundleExtra("android.intent.extra.info");
if (infod == null) {
is_valid = false;
} else {
_id = infod.getInt("id");
_name = infod.getString("name");
is_valid = true;
}
} else {
is_valid = false;
}
}


 


 


@Override
public void run() {
switch (_id) {
case MainActivity.CPU_INFO:
info_datas = FetchData.fetch_cpu_info();
break;
case MainActivity.DISK_INFO:
info_datas = FetchData.fetch_disk_info();
break;
case MainActivity.MEM_INFO:
info_datas = FetchData.getMemoryInfo(this);
break;
case MainActivity.DisplayMetrics:
info_datas = FetchData.getDisplayMetrics(this);
break;
}


handler.sendEmptyMessage(0);
}


private Handler handler = new Handler() {
public void handleMessage(Message msg) {
pd.dismiss();
info.setText(info_datas);
}
};


}


--------------------------FetchData.java-----------------


package com.example.hardware;


import java.io.IOException;


import android.app.ActivityManager;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;


public class FetchData {
private static StringBuffer buffer;


// cpu info
public static String fetch_cpu_info() {
String result = null;
CMDExecute cmdexe = new CMDExecute();
try {
String[] args = { "/system/bin/cat", "/proc/cpuinfo" };
result = cmdexe.run(args, "/system/bin/");
Log.i("result", "result=" + result);
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}


// disk info
public static String fetch_disk_info() {
String result = null;
CMDExecute cmdexe = new CMDExecute();
try {
String[] args = { "/system/bin/df" };
result = cmdexe.run(args, "/system/bin/");
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}






/**
* 系统内存情况查看
*/
public static String getMemoryInfo(Context context) {
StringBuffer memoryInfo = new StringBuffer();
final ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);

ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(outInfo);
memoryInfo.append("\nTotal Available Memory :").append(
outInfo.availMem >> 10).append("k");
memoryInfo.append("\nTotal Available Memory :").append(
outInfo.availMem >> 20).append("M");
memoryInfo.append("\nIn low memory situation:").append(
outInfo.lowMemory);

String result = null;
CMDExecute cmdexe = new CMDExecute();
try {
String[] args = { "/system/bin/cat","/proc/meminfo" };
result = cmdexe.run(args, "/system/bin/");
} catch (IOException ex) {
Log.i("fetch_process_info", "ex=" + ex.toString());
}

return memoryInfo.toString()+"\n\n"+result;
}





/**
* 系统信息查看方法
*/
public static String getSystemProperty() {
buffer = new StringBuffer();
initProperty("java.vendor.url", "java.vendor.url");
initProperty("java.class.path", "java.class.path");
initProperty("user.home", "user.home");
initProperty("java.class.version", "java.class.version");
initProperty("os.version", "os.version");
initProperty("java.vendor", "java.vendor");
initProperty("user.dir", "user.dir");
initProperty("user.timezone", "user.timezone");
initProperty("path.separator", "path.separator");
initProperty(" os.name", " os.name");
initProperty("os.arch", "os.arch");
initProperty("line.separator", "line.separator");
initProperty("file.separator", "file.separator");
initProperty("user.name", "user.name");
initProperty("java.version", "java.version");
initProperty("java.home", "java.home");
return buffer.toString();
}


private static String initProperty(String description, String propertyStr) {
if (buffer == null) {
buffer = new StringBuffer();
}
buffer.append(description).append(":");
buffer.append(System.getProperty(propertyStr)).append("\n");
return buffer.toString();
}


public static String getDisplayMetrics(Context cx) {
String str = "";
DisplayMetrics dm = new DisplayMetrics();
dm = cx.getApplicationContext().getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
float density = dm.density;
float xdpi = dm.xdpi;
float ydpi = dm.ydpi;
str += "The absolute width:" + String.valueOf(screenWidth) + "pixels\n";
str += "The absolute heightin:" + String.valueOf(screenHeight)
+ "pixels\n";
str += "The logical density of the display.:" + String.valueOf(density)
+ "\n";
str += "X dimension :" + String.valueOf(xdpi) + "pixels per inch\n";
str += "Y dimension :" + String.valueOf(ydpi) + "pixels per inch\n";
return str;
}


}


------------------------CDMExecute.java-------------------------

package com.example.hardware;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;


public class CMDExecute {


public synchronized String run(String[] cmd, String workdirectory)
throws IOException {
String result = "";


try {
ProcessBuilder builder = new ProcessBuilder(cmd);
// set working directory
if (workdirectory != null)
builder.directory(new File(workdirectory));
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream in = process.getInputStream();
byte[] re = new byte[1024];
while (in.read(re) != -1) {
System.out.println(new String(re));
result = result + new String(re);
}
in.close();


} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}


}

。。。。。。。。。main.xml。。。。。。。。。。。。。。


android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">


   android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:id="@+id/itemlist" />
 


。。。。。。。。。。。/res/layout/info_row.xml。。。。。。。。。。。。


    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vw1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="4px"
    android:orientation="horizontal">    
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">


                    android:textSize="18sp"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>


                    android:textSize="14sp"
            android:layout_width="fill_parent"
            android:paddingLeft="20px"
            android:layout_height="wrap_content"/>


   




.......。。。。。。。。。。。。。。/res/layout/showinfo.xml。。。。。。。。。。。。。。


        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20px"
>


android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:textSize="20sp" 
android:paddingBottom="8dip"
android:text="" />

android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:text="" />




25、显示硬件信息(cpu信息、内存信息、硬盘信息、显示屏信息)_第1张图片

25、显示硬件信息(cpu信息、内存信息、硬盘信息、显示屏信息)_第2张图片

25、显示硬件信息(cpu信息、内存信息、硬盘信息、显示屏信息)_第3张图片

25、显示硬件信息(cpu信息、内存信息、硬盘信息、显示屏信息)_第4张图片

25、显示硬件信息(cpu信息、内存信息、硬盘信息、显示屏信息)_第5张图片

你可能感兴趣的:(android项目)