Android系统信息获取 之二:版本信息获取
获取当前应用的版本号:
private String getAppVersionName() throws Exception
{
// 获取packagemanager的实例
PackageManager packageManager = getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(),PackageManager.GET_ACTIVITIES);
String version = packInfo.versionName;
return version;
}
获取对应的服务的版本信息:
private String getServiceVersionName() throws Exception
{
// 获取packagemanager的实例
PackageManager packageManager = getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(),PackageManager. GET_SERVICES);
String version = packInfo.versionName;
return version;
}
可以看到这两个获取信息的差别在于getPackageInfo的第二个参数,查看Android系统中PackageManager.java的代码,就会发现除了GET_ACTIVITIES, GET_SERVICES之外还有GET_RECEIVERS、GET_PROVIDERS、GET_GIDS等属性,由此可知要想获取这些属性的信息,均可通过上边的方法获取。
获取当前系统的版本号:
textView.setText("Product Model: " + android.os.Build.MODEL + ","
+ android.os.Build.VERSION.SDK + ","
+ android.os.Build.VERSION.RELEASE);
Android系统Build的属性均在Build.java这个文件中。
其中系统版本的代码可在其中查到,如下:
/*
*
* Build.VERSION_CODES
(0x00000001) Android 1.0 BASE
(0x00000002) Android 1.1 BASE_1_1
(0x00000003) Android 1.5 CUPCAKE
(0x00000004) Android 1.6 DONUT
(0x00000005) Android 2.0 ECLAIR
(0x00000006) Android 2.0.1 ECLAIR_0_1
(0x00000007) Android 2.1 ECLAIR_MR1
(0x00000008) Android 2.2 FROYO
(0x00000009) Android 2.3 GINGERBREAD
(0x0000000a) Android 2.3.3 GINGERBREAD_MR1
(0x0000000b) Android 3.0 HONEYCOMB
(0x0000000c) Android 3.1 HONEYCOMB_MR1
(0x0000000d) Android 3.2 HONEYCOMB_MR2
(0x0000000e) Android 4.0 ICE_CREAM_SANDWICH
(0x0000000f) Android 4.0.3 ICE_CREAM_SANDWICH_MR4
*/
/*获取当前系统的android版本号*/
int currentapiVersion=android.os.Build.VERSION.SDK_INT;
通过build.java文件获取其他信息,可参考下面代码:
String phoneInfo = "Product: " + android.os.Build.PRODUCT;
phoneInfo += ", CPU_ABI: " + android.os.Build.CPU_ABI;
phoneInfo += ", TAGS: " + android.os.Build.TAGS;
phoneInfo+=",VERSION_CODES.BASE:"+ android.os.Build.VERSION_CODES.BASE;
phoneInfo += ", MODEL: " + android.os.Build.MODEL;
phoneInfo += ", SDK: " + android.os.Build.VERSION.SDK;
phoneInfo += ", VERSION.RELEASE: " + android.os.Build.VERSION.RELEASE;
phoneInfo += ", DEVICE: " + android.os.Build.DEVICE;
phoneInfo += ", DISPLAY: " + android.os.Build.DISPLAY;
phoneInfo += ", BRAND: " + android.os.Build.BRAND;
phoneInfo += ", BOARD: " + android.os.Build.BOARD;
phoneInfo += ", FINGERPRINT: " + android.os.Build.FINGERPRINT;
phoneInfo += ", ID: " + android.os.Build.ID;
phoneInfo += ", MANUFACTURER: " + android.os.Build.MANUFACTURER;
phoneInfo += ", USER: " + android.os.Build.USER;
// Toast.makeText(this, phoneInfo, Toast.LENGTH_LONG).show();
TextView mTextView = (TextView) findViewById(R.id.main_phoneinfo);
mTextView.setText(phoneInfo);