android 利用反射获取当前系统sdk版本等属性

在android系统中,/system/build.prop中含有大量系统相关的信息:

# begin build properties
# autogenerated by buildinfo.sh
ro.build.id=FRF91
ro.build.display.id=sdk-eng 2.2 FRF91 43546 test-keys
ro.build.version.incremental=43546
ro.build.version.sdk=8
ro.build.version.codename=REL
ro.build.version.release=2.2
ro.build.date=Wed Jun 30 13:58:00 PDT 2010
ro.build.date.utc=1277931480
ro.build.type=eng
ro.build.user=android-build
ro.build.host=android-test-25.mtv.corp.google.com
ro.build.tags=test-keys
ro.product.model=sdk
ro.product.brand=generic
ro.product.name=sdk
ro.product.device=generic
ro.product.board=
ro.product.cpu.abi=armeabi
ro.product.manufacturer=unknown
ro.product.locale.language=ldpi
ro.product.locale.region=
ro.wifi.channels=
ro.board.platform=
# ro.build.product is obsolete; use ro.product.device
ro.build.product=generic
# Do not try to parse ro.build.description or .fingerprint
ro.build.description=sdk-eng 2.2 FRF91 43546 test-keys
ro.build.fingerprint=generic/sdk/generic/:2.2/FRF91/43546:eng/test-keys
# end build properties
#
# system.prop for generic sdk
#

rild.libpath=/system/lib/libreference-ril.so
rild.libargs=-d /dev/ttyS0

#
# ADDITIONAL_BUILD_PROPERTIES
#
ro.config.notification_sound=OnTheHunt.ogg
ro.config.alarm_alert=Alarm_Classic.ogg
media.stagefright.enable-player=true
media.stagefright.enable-meta=true
media.stagefright.enable-scan=true
media.stagefright.enable-http=true
ro.kernel.android.checkjni=1
ro.setupwizard.mode=OPTIONAL
xmpp.auto-presence=true
ro.config.nocheckin=yes
net.bt.name=Android
dalvik.vm.stack-trace-file=/data/anr/traces.txt

想要针对不同sdk版本,进行不同的处理,就要获取sdk的版本,即ro.build.version.sdk对应的值,本来想用下面的方法获取属性值,但是获取不成功:

System.getenv("ro.build.version.sdk")

 在系统源码中发现SystemProperties可以获取这些属性值,但是SystemProperties没有公布出来,于是想到用反射的办法来调用SystemProperties的方法:

package org.ckl.reflect;

import java.lang.reflect.Method;

import android.util.Log;

public class MySystemProperties {
	private static final String TAG = "MySystemProperties";

	// String SystemProperties.get(String key){}
	public static String get(String key) {
		init();
		
		String value = null;
		
		try {
			value = (String) mGetMethod.invoke(mClassType, key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return value;
	}
	
	//int SystemProperties.get(String key, int def){}
	public static int getInt(String key, int def) {
		init();
		
		int value = def;
		try {
			Integer v = (Integer) mGetIntMethod.invoke(mClassType, key, def);
			value = v.intValue();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}
	
	public static int getSdkVersion() {
		return getInt("ro.build.version.sdk", -1);
	}
	
	//-------------------------------------------------------------------
	private static Class<?> mClassType = null;
	private static Method mGetMethod = null;
	private static Method mGetIntMethod = null;
	private static void init() {
		try {
			if (mClassType == null) {
				mClassType = Class.forName("android.os.SystemProperties");
				
				mGetMethod = mClassType.getDeclaredMethod("get", String.class);
				mGetIntMethod = mClassType.getDeclaredMethod("getInt", String.class, int.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

上面的方法确实可以获取到sdk的版本:

		Log.i(TAG, MySystemProperties.get("ro.build.version.sdk"));
		Log.i(TAG, "" + MySystemProperties.getInt("ro.build.version.sdk", -1));


你可能感兴趣的:(android 利用反射获取当前系统sdk版本等属性)