24、获取系统信息(包括操作系统版本、系统信息、运营商信息)

2024-05-25 07:18

本文主要是介绍24、获取系统信息(包括操作系统版本、系统信息、运营商信息),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

github上完整项目:https://github.com/shixinga/1-show-the-phone-message.git


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


package com.example.tg;


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.util.Log;
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 VER_INFO = 1;
public static final int SystemProperty = 2;
public static final int TEL_STATUS = 3;
ListView itemlist = null;
List<Map<String, Object>> list;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.infos);
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.VER_INFO);
map.put("name", "操作系统版本");
map.put("desc", "读取/proc/version信息");
list.add(map);



map = new HashMap<String, Object>();
map.put("id", MainActivity.SystemProperty);
  map.put("name", "系统信息");
map.put("desc", "手机设备的系统信息.");
// map.put("icon", R.drawable.mem);
list.add(map);


map = new HashMap<String, Object>();
map.put("id", MainActivity.TEL_STATUS);
  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);
startActivity(intent);
}
}


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


package com.example.tg;


 
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);
//ActionBar的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.VER_INFO:
info_datas = FetchData.fetch_version_info();
break;
case MainActivity.TEL_STATUS:
info_datas = FetchData.fetch_tel_status(this);
break;
case MainActivity.SystemProperty:
info_datas = FetchData.getSystemProperty();
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.tg;


import java.io.IOException;
import java.util.Iterator;
import java.util.List;


import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;


public class FetchData {
private static StringBuffer buffer;


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


public static String fetch_tel_status(Context cx) {
String result = null;
TelephonyManager tm = (TelephonyManager) cx
.getSystemService(Context.TELEPHONY_SERVICE);//    
String str = "";
str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion()
+ "\n";
str += "Line1Number = " + tm.getLine1Number() + "\n";
str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";
str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";
str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";
str += "NetworkType = " + tm.getNetworkType() + "\n";
str += "PhoneType = " + tm.getPhoneType() + "\n";
str += "SimCountryIso = " + tm.getSimCountryIso() + "\n";
str += "SimOperator = " + tm.getSimOperator() + "\n";
str += "SimOperatorName = " + tm.getSimOperatorName() + "\n";
str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n";
str += "SimState = " + tm.getSimState() + "\n";
str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";
str += "VoiceMailNumber = " + tm.getVoiceMailNumber() + "\n";


int mcc = cx.getResources().getConfiguration().mcc;
int mnc = cx.getResources().getConfiguration().mnc;
str += "IMSI MCC (Mobile Country Code):" + String.valueOf(mcc) + "\n";
str += "IMSI MNC (Mobile Network Code):" + String.valueOf(mnc) + "\n";
result = str;
return 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();
}


}


----------------------------------CMDExecute.java-------------------------------


package com.example.tg;


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"?>
<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>


、、、、权限、、、、、、、

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<activity android:name=".ShowInfoActivity"/>





这篇关于24、获取系统信息(包括操作系统版本、系统信息、运营商信息)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1000832

相关文章

Windows系统宽带限制如何解除?

《Windows系统宽带限制如何解除?》有不少用户反映电脑网速慢得情况,可能是宽带速度被限制的原因,只需解除限制即可,具体该如何操作呢?本文就跟大家一起来看看Windows系统解除网络限制的操作方法吧... 有不少用户反映电脑网速慢得情况,可能是宽带速度被限制的原因,只需解除限制即可,具体该如何操作呢?本文

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

电脑找不到mfc90u.dll文件怎么办? 系统报错mfc90u.dll丢失修复的5种方案

《电脑找不到mfc90u.dll文件怎么办?系统报错mfc90u.dll丢失修复的5种方案》在我们日常使用电脑的过程中,可能会遇到一些软件或系统错误,其中之一就是mfc90u.dll丢失,那么,mf... 在大部分情况下出现我们运行或安装软件,游戏出现提示丢失某些DLL文件或OCX文件的原因可能是原始安装包

电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案

《电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案》最近有不少兄弟反映,电脑突然弹出“mfc100u.dll已加载,但找不到入口点”的错误提示,导致一些程序无法正... 在计算机使用过程中,我们经常会遇到一些错误提示,其中最常见的就是“找不到指定的模块”或“缺少某个DL

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代