PHP
·
发表于 5年以前
·
阅读量:8410
public static Properties collectDeviceInfo(Context context) {
Properties mDeviceCrashInfo = new Properties();
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(context.getPackageName(),
PackageManager.GET_ACTIVITIES);
if (pi != null ) {
mDeviceCrashInfo.put(VERSION_NAME,
pi.versionName == null ? "not set" : pi.versionName);
mDeviceCrashInfo.put(VERSION_CODE, pi.versionCode);
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Error while collect package info" , e);
}
Field[] fields = Build. class .getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible( true );
mDeviceCrashInfo.put(field.getName(), field.get( null ));
} catch (Exception e) {
Log.e(TAG, "Error while collect crash info" , e);
}
}
return mDeviceCrashInfo;
}
public static String collectDeviceInfoStr(Context context) {
Properties prop = collectDeviceInfo(context);
Set deviceInfos = prop.keySet();
StringBuilder deviceInfoStr = new StringBuilder( "{\n" );
for (Iterator iter = deviceInfos.iterator(); iter.hasNext();) {
Object item = iter.next();
deviceInfoStr.append( "\t\t\t" + item + ":" + prop.get(item)
+ ", \n" );
}
deviceInfoStr.append( "}" );
return deviceInfoStr.toString();
}