歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 獲取Android各類系統相關信息的接口實現代碼

獲取Android各類系統相關信息的接口實現代碼

日期:2017/3/1 9:58:00   编辑:Linux編程

獲取Android各類系統相關信息的接口實現代碼

/**
* 獲取系統中所有安裝包信息
*/
public String getAllPackages(Context context)
{
String appList = "";
List<PackageInfo> 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;
}

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved