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

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,然后保存起来。这个算是兼容方案,当然这样的设备并不会很多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值