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<Map<String, Object>> 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<Map<String, Object>> buildListForSimpleAdapter() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(3);
// Build a map for the attributes
Map<String, Object> map = new HashMap<String, Object>();

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



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


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

 
map = new HashMap<String, Object>();
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<String, Object> 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。。。。。。。。。。。。。。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">


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


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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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">    
   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <TextView android:id="@+id/name"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>


        <TextView android:id="@+id/desc"
            android:textSize="14sp"
            android:layout_width="fill_parent"
            android:paddingLeft="20px"
            android:layout_height="wrap_content"/>


    </LinearLayout>


</LinearLayout>


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

<?xml version="1.0" encoding="utf-8"?>

<!--红色字体不能省,否则当显示内存信息的时候(内存信息量多),会显示不完整!!!-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20px"
>


<TextView android:id="@+id/title" 
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:textSize="20sp" 
android:paddingBottom="8dip"
android:text="" />

<TextView android:id="@+id/info" 
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:text="" />

</LinearLayout>
</ScrollView>







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Windows 中,可以使用 "systeminfo" 命令在命令提示符 (cmd) 中查看电脑硬件信息。 打开 cmd,输入 "systeminfo" 并回车,即可查看电脑的硬件信息,包括硬件配置、系统信息、网络配置等。 也可以使用 "dxdiag" 命令查看显卡、声卡等硬件信息。 如果需要更详细的信息,可以使用 "msinfo32" 命令查看系统信息,还可以查看软件配置和硬件资源使用情况。 ### 回答2: 要使用CMD命令查看电脑硬件信息,可以按照以下步骤进行: 1. 打开CMD命令提示符:在Windows操作系统中,可以按下Win键 + R,然后输入“cmd”来打开CMD命令提示符。 2. 查看处理器信息:在CMD窗口中,输入“wmic cpu get Name, Manufacturer, MaxClockSpeed”命令并按下回车键,将显示处理器的名称、制造商和最大时钟速度。 3. 查看内存信息:在CMD窗口中,输入“wmic memorychip get Manufacturer, Speed, Capacity”命令并按下回车键,将显示内存芯片的制造商、速度和容量。 4. 查看硬盘信息:在CMD窗口中,输入“wmic diskdrive get Manufacturer, Model, Size”命令并按下回车键,将显示硬盘驱动器的制造商、型号和大小。 5. 查看显示信息:在CMD窗口中,输入“wmic desktopmonitor get Manufacturer, Model, ScreenHeight, ScreenWidth”命令并按下回车键,将显示显示器的制造商、型号、屏幕高度和宽度。 6. 查看显卡信息:在CMD窗口中,输入“wmic path Win32_VideoController get Name, AdapterRAM”命令并按下回车键,将显示显卡的名称和适配器RAM。 7. 查看网络适配器信息:在CMD窗口中,输入“wmic nicconfig get Description, MACAddress”命令并按下回车键,将显示网络适配器的描述和MAC地址。 通过以上步骤,你可以使用CMD命令查看电脑的处理器、内存硬盘显示器、显卡和网络适配器等硬件信息。 ### 回答3: 在Windows操作系统中,我们可以使用cmd命令来查看电脑硬件信息。下面是一些常用的命令和对应的功能: 1. systeminfo:使用该命令可以显示电脑的详细硬件和操作系统信息,包括处理器、内存、操作系统版本等。 2. wmic cpu list brief:这个命令可以显示处理器的详细信息,如型号、核心数、频率等。 3. wmic memorychip list brief:该命令用于显示内存条的信息,包括容量、型号、生产厂商等。 4. wmic diskdrive list brief:这个命令可以列出所有硬盘驱动器的信息,包括容量、型号、接口类型等。 5. wmic bios list brief:使用该命令可以查看BIOS的信息,如制造商、版本、发布日期等。 6. wmic baseboard list brief:这个命令可以显示主板的信息,包括制造商、型号、序列号等。 7. wmic sounddev list brief:该命令用于显示音频设备的信息,包括输入输出设备、制造商等。 8. wmic nic list brief:使用该命令可以查看网络适配器的信息,包括制造商、型号、MAC地址等。 以上是一些常用的cmd命令来获取电脑硬件信息的例子。使用这些命令,我们可以更好地了解我们的电脑硬件配置,以便在需要时进行相应的调整或升级。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值