linux 系统接口代码,获取Android各类系统相关信息的接口实现代码

获取Android各类系统相关信息的接口实现代码

/**

*  获取系统中所有安装包信息

*/

public String getAllPackages(Context context)

{

String appList = "";

List packages = context.getPackageManager().getInstalledPackages(0);

for (PackageInfo packageInfo : packages)

{

// 这里可以控制是否需要获取系统级别的package

if ((packageInfo.applicationInfo.flags &  ApplicationInfo.FLAG_SYSTEM) == 0)

{

appList += packageInfo.applicationInfo.loadLabel(getPackageManager()).toString() + ",";

}

}

return appList.substring(0, appList.length()-1);

}

/**

*  获取系统内存信息

*/

public String getSysMemoryInfo(Context context)

{

long freeMem = 0;

long totalMem = 0;

ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);

MemoryInfo memoryInfo = new MemoryInfo();

activityManager.getMemoryInfo(memoryInfo);

freeMem = memoryInfo.availMem/1024/1024;

//读取指定文件信息

String path = "/proc/meminfo";

String content = null;

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader(path), 8);

String line;

if ((line = br.readLine()) != null) {

content = line;

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (br != null) {

try {

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

// beginIndex

int begin = content.indexOf(':');

// endIndex

int end = content.indexOf('k');

// 截取字符串信息

content = content.substring(begin + 1, end).trim();

totalMem = Integer.parseInt(content)/1024;

return freeMem+" "+totalMem;

}

/**

* 获取CPU信息

*/

private String getCpuInfo() {

String str1 = "/proc/cpuinfo";

String str2 = "";

String cpuInfo="";  //1-cpu型号  //2-cpu频率

String[] arrayOfString;

try {

FileReader fr = new FileReader(str1);

BufferedReader localBufferedReader = new BufferedReader(fr, 8192);

str2 = localBufferedReader.readLine();

arrayOfString = str2.split("\\s+");

for (int i = 2; i < arrayOfString.length; i++) {

cpuInfo =  arrayOfString[i] + "  ";

}

str2 = localBufferedReader.readLine();

arrayOfString = str2.split("\\s+");

cpuInfo =cpuInfo + arrayOfString[2];

localBufferedReader.close();

} catch (IOException e) {

}

return cpuInfo;

}

/**

* 获取MAC地址

*/

private String getMacAddress(){

String result = "";

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

result = wifiInfo.getMacAddress();

// Log.i(TAG, "macAdd:" + result);

return result;

}

/**

* 获取其他手机信息

*/

private String getInfo() {

TelephonyManager mTm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

String imei = mTm.getDeviceId();

String imsi = mTm.getSubscriberId();

String mtype = android.os.Build.MODEL; // 手机型号

String numer = mTm.getLine1Number(); // 手机号码

return imei + " " + imsi + " " + mtype + " " + numer;

}

/**

* 获取屏幕的分辨率

*/

private String getWeithAndHeight(){

//这种方式在service中无法使用,

DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

int width = dm.widthPixels;              //宽

int height = dm.heightPixels;          //高

//在service中也能得到高和宽

WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

int width0 = mWindowManager.getDefaultDisplay().getWidth();

int height0 = mWindowManager.getDefaultDisplay().getHeight();

return ""+width +" "+height + " " +width0 + " " +height0;

}

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值