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

来源:互联网 发布:php商城系统下载 编辑:程序博客网 时间:2024/05/06 12: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"/>





0 0