您的位置:首页 > 移动开发 > Android开发

android获取版本信息、屏幕信息和设备编号

2014-08-01 12:21 429 查看
1、获取版本信息:

/**
* 获取version
*/
try {
PackageInfo info=getPackageManager().getPackageInfo(this.getPackageName(), 1);
Log.i("info","info.versionCode:"+info.versionCode);
Log.i("info","info.versionName:"+info.versionName);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


2、获取屏幕信息

/**
* 获取屏幕信息
*/
DisplayMetrics dm=getResources().getDisplayMetrics();
Log.i("info","widthPixels:"+dm.widthPixels);
Log.i("info","heightPixels:"+dm.heightPixels);


3、获取设备编号

public static String getDeviceId(Context context) {
String deviceId = null;
deviceId = ((TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
if (deviceId == null && Build.VERSION.SDK_INT > 9) {
deviceId = Secure.getString(context.getContentResolver(),
Secure.ANDROID_ID);
if (deviceId == null) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null
&& networkInfo.isAvailable()
&& networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
WifiManager wm = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
deviceId = wm.getConnectionInfo().getMacAddress();
} else {
deviceId = UUID.randomUUID().toString();
}
}
}

if (deviceId != null && deviceId.length() < 28) {
int len = 28 - deviceId.length();
for (int i = 0; i < len; i++) {
deviceId = "0" + deviceId;
}
}

return deviceId;
}


3.1、基本上,所有设备通过((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();获取一个设备编号。但是某些平板电脑会返回空。

3.2、通过 Secure.getString(s_instance.getContentResolver(), Secure.ANDROID_ID); 会返回一个android系统唯一区分的64-bit 哈希字符串。但是android2.2或者是某些山寨手机使用这个也是有问题的,它会返回一个固定的值 9774d56d682e549c。

3.3、如果用户链接上Wi-Fi,则可以获取用户的MacAddress。

3.4、如果前两个都没有获取到udid,那么就在程序启动的时候创建一个随机的uuid,然后保存起来。这个算是兼容方案,当然这样的设备并不会很多。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: