Android获取终端信息

package jp.co.sumoretece.android.mobileinfo;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class InfoCollection extends Activity {
	private static StringBuffer buffer;
    TextView infos;
    TextView title;
    List list2;
    List list3;
    List list4;
    protected Menu myMenu;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showinfo);
        refreshListItems();
    }
    
    private void refreshListItems() {
        infos = (TextView) findViewById(R.id.info);
        title = (TextView) findViewById(R.id.title);
        title.setText("查看信息");
        TextView blight = (TextView) findViewById(R.id.blight);
        float light = getOldBrightness();
        blight.setText("/n======/nbrightness:" + light);
        String i = getTelephonyManager(this);
        String tel = i;
        
        String ii = getActivityManager();
        String aam = ii;
        
        String iii = getCpu();
        String cpu = iii + "/n======/n";
        
        String wifi = getWifiInfo();
        String si = getNetWorkIP();
        String network = si + "/n======/n";
        
        boolean ss = isConnected();
        
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText("/n======/nisConnected():" + ss + "/n");
        
        infos.setText(tel.toString() + aam.toString() + wifi.toString() + network.toString() + cpu.toString());
    }
    
    /**
     * 获取TelephonyManager
     *
     */
    
    public static String getTelephonyManager(Context cx) {
        String result = null;
        TelephonyManager tm = (TelephonyManager) cx.getSystemService(Context.TELEPHONY_SERVICE);
        String str = "/n======/nTelephonyManager/n";
        str += "getDataState() = " + tm.getDataState() + "/n";
        str += "getDeviceId() = " + tm.getDeviceId() + "/n";
        str += "getLine1Number() = " + tm.getLine1Number() + "/n";
        str += "getSimSerialNumber() = " + tm.getSimSerialNumber() + "/n";
        str += "getSubscriberId() = " + tm.getSubscriberId() + "/n";
        str += "isNetworkRoaming() = " + tm.isNetworkRoaming() + "/n";
        result = str.toString();
        return result;
    }

    /**
     * 获取获取Wifi
     *
     */
    public String getWifiInfo() {
        WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        String s1 = "/n======/nWifiState:/nWIFI_STATE_UNKNOWN/n";
        String s21 = "/n======/nWifiState:/nWIFI_STATE_KNOWN/n";
        StringBuilder stringbuilder = new StringBuilder("/nState:");
        if (wifiManager.getWifiState() != 3) {
            String s2 = s1;
            String s3 = stringbuilder.append(s2).toString();
            String s4 = s3;
            return s4;
        }
        else {
            return s21.toString();
        }
    }

    /**
     * 获取ActivityManager
     *
     */
    public String getActivityManager() {
        String result = null;
        StringBuffer str = new StringBuffer("/n======/nActivityManager/n");
        ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List list1 = activityManager.getRunningAppProcesses();
        for (int i = 0; i < list1.size(); i++) {
            String b = list1.get(i).toString();
            str.append(b + "/n");
        }
        return str.toString();
    }

    /**
     * 获取当前亮度值
     *
     */
    private float getOldBrightness() {
        float brightness;
        try {
            brightness = Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS);
        }
        catch (SettingNotFoundException snfe) {
            brightness = 255;
        }
        return brightness;
    }

    /**
     * 获取Cpu
     *
     */
    private String getCpu() {
        ProcessBuilder cmd;
        String result = "";
        try {
            String[] args = { "/system/bin/cat", "/proc/cpuinfo" };
            cmd = new ProcessBuilder(args);
            Process process = cmd.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);
                return result.toString();
            }
            in.close();
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        return result.toString();
    }

    /**
     * 获取NetWorkIP
     *
     */
    public static String getNetWorkIP() {
        String result = "/n======/n";
        CMDExecute cmdexe = new CMDExecute();
        try {
            String[] args = { "/system/bin/netcfg" };
            result = cmdexe.run(args, "/system/bin/");
        }
        catch (IOException ex) {
            Log.i("fetch_process_info", "ex=" + ex.toString());
        }
        return result;
    }

    /**
     * 获取isConnected()
     *
     */
     public boolean isConnected() {
         ConnectivityManager mConnectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
         TelephonyManager mTelephony = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
         
         // 检查网络连接,如果无网络可用,就不需要进行连网操作等
         NetworkInfo info = mConnectivity.getActiveNetworkInfo();
         if (info == null || !mConnectivity.getBackgroundDataSetting()) {
         	return false;
         } // 判断网络连接类型,只有在3G或wifi里进行一些数据更新。
         int netType = info.getType();
         int netSubtype = info.getSubtype();
         if (netType == ConnectivityManager.TYPE_WIFI) {
         	return info.isConnected();
         }
         else if (netType == ConnectivityManager.TYPE_MOBILE && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) {
         	return info.isConnected();
         }
         else {
         	return false;
         }
     }
}
暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心