android 获取设备信息

来源:互联网 发布:儿童冬天服装淘宝网 编辑:程序博客网 时间:2024/05/18 22:40

 

 

android.os.Build设备常量以及imsi号与ip地址的获得

android.os.Build.MODEL :设备名
android.os.Build.BRAND: 设备厂商
android.os.Build.VERSION.SDK:sdk版本号
一般用于版本兼容的检测或者其他功能
比如说coolpad 9930手机2.2的android系统
择MODEL为9930,BRAND为coolpad,sdk为8
imsi号获得
(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE).getSubscriberId();
获取ip地址


 

1
2
3
4
5
6
7
8
9
10
11
12
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);      
WifiInfo wifiInfo = wifiManager.getConnectionInfo();      
int ipAddress = wifiInfo.getIpAddress();      
String ip = intToIp(ipAddress);      
       
public String intToIp(int i) {      
       
   return ((i >> 24 ) & 0xFF ) + "." +      
               ((i >> 16 ) & 0xFF) + "." +      
               ((i >> 8 ) & 0xFF) + "." +      
               ( i & 0xFF) ;      
}

 Android 关于android.os.Build介绍

关于Build类的介绍

这个类为一个获取设备一些初始化信息的类,该类的主要信息都是通过一些static的字段获得:

public static final StringBOARDThe name of the underlying board, like "goldfish".(设备厂商)public static final StringBOOTLOADERThe system bootloader version number.public static final StringBRANDThe brand (e.g., carrier) the software is customized for, if any.public static final StringCPU_ABIThe name of the instruction set (CPU type + ABI convention) of native code.public static final StringCPU_ABI2The name of the second instruction set (CPU type + ABI convention) of native code.public static final StringDEVICEThe name of the industrial design.public static final StringDISPLAYA build ID string meant for displaying to the userpublic static final StringFINGERPRINTA string that uniquely identifies this build.public static final StringHARDWAREThe name of the hardware (from the kernel command line or /proc).public static final StringHOST public static final StringIDEither a changelist number, or a label like "M4-rc20".public static final StringMANUFACTURERThe manufacturer of the product/hardware.public static final StringMODELThe end-user-visible name for the end product.(设备名)public static final StringPRODUCTThe name of the overall product.public static final StringRADIOThis field is deprecated. The radio firmware version is frequently not available when this class is initialized, leading to a blank or "unknown" value for this string. Use getRadioVersion() instead.public static final StringSERIALA hardware serial number, if available.public static final StringTAGSComma-separated tags describing the build, like "unsigned,debug".public static final longTIME public static final StringTYPEThe type of build, like "user" or "eng".public static final StringUSER

 

 

 

//获取当前ANDRROID 版本

 public static int getApiLevel() {

int currentApi = 0;
  if (currentApi > 0) {
   return currentApi;
  }
  try {
   currentApi = Integer.valueOf(android.os.Build.VERSION.SDK);
  } catch (NumberFormatException e) {
   currentApi = 0;
  }
  return currentApi;
 }

 

//判断机型实例

 public static boolean shouldChangeConnectForBluetooth(){
  if (android.os.Build.MODEL.equalsIgnoreCase("GT-I9300")) {
   return true;
  }
  if (android.os.Build.MODEL.equalsIgnoreCase("GT-N8000")) {
   return true;
  }
  return false;
 }

 

 public static boolean shouldUseModeApi() {

  // Samsung GT-I5508
  if (android.os.Build.DEVICE.equalsIgnoreCase("GT-I5508")) {
   return true;
  }

}


 

  // Motorola milestone 1 and 2 & motorola droid
  if (android.os.Build.DEVICE.toLowerCase().contains("milestone2")
    || android.os.Build.BOARD.toLowerCase().contains("sholes")
    || android.os.Build.PRODUCT.toLowerCase().contains("sholes")) {
   return true;
  }

 

  if (android.os.Build.BRAND.toLowerCase().startsWith("dell")
    && android.os.Build.DEVICE.equalsIgnoreCase("streak")) {
   return true;
  }

 

 public static String getCpuAbi() {
  if (isCompatible(4)) {
   Field field;
   try {
    field = android.os.Build.class.getField("CPU_ABI");
    return field.get(null).toString();
   } catch (Exception e) {
    Log.w(THIS_FILE,
      "Announce to be android 1.6 but no CPU ABI field", e);
   }

  }
  return "armeabi";
 }

 

 

 

3)获取AndroidManifest.xml的信息
1.versionCode
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
getPackageManager().getPackageInfo(packageName, 0).versionCode
getPackageManager().getPackageInfo(packageName, 0).versionCode
可以用Context.gerPackageName()取得packageName
2.versionName
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
getPackageManager().getPackageInfo(packageName, 0).versionName
getPackageManager().getPackageInfo(pName, PackageManager.GET_CONFIGURATIONS);
getPackageManager().getPackageInfo(packageName, 0).versionName
getPackageManager().getPackageInfo(pName, PackageManager.GET_CONFIGURATIONS);


4)Android判断应用是否存在
1.通过包名判断
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
public boolean checkBrowser(String packageName) {
if (packageName == null || "".equals(packageName))
return false;
try {
ApplicationInfo info = getPackageManager().getApplicationInfo(
packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
public boolean checkBrowser(String packageName) {
if (packageName == null || "".equals(packageName))
return false;
try {
ApplicationInfo info = getPackageManager().getApplicationInfo(
packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}

2.通过Activity判断
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.android.settings", //$NON-NLS-1$
"com.android.settings.InstalledAppDetails"); //$NON-NLS-1$
intent.putExtra("com.android.settings.ApplicationPkgName", //$NON-NLS-1$
mCurrentPkgName);
List acts = getPackageManager().queryIntentActivities(
intent, 0);
if (acts.size() > 0) {
startActivity(intent);
} else {
Toast.makeText(this,
getString(R.string.failed_to_resolve_activity),
Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.android.settings", //$NON-NLS-1$
"com.android.settings.InstalledAppDetails"); //$NON-NLS-1$
intent.putExtra("com.android.settings.ApplicationPkgName", //$NON-NLS-1$
mCurrentPkgName);
List acts = getPackageManager().queryIntentActivities(
intent, 0);
if (acts.size() > 0) {
startActivity(intent);
} else {
Toast.makeText(this,
getString(R.string.failed_to_resolve_activity),
Toast.LENGTH_SHORT).show();
}

5)获取设备屏幕分辨率
首先我们需要用到的是DisplayMetrics这个类,它可以为我们获得手机屏幕属性,这里将其所在类导入。
view plaincopy to clipboardprint?
import android.util.DisplayMetrics;
import android.util.DisplayMetrics;
得到实例对象。
view plaincopy to clipboardprint?
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
得到手机屏幕高度:
view plaincopy to clipboardprint?
dm.heightPixels;
dm.heightPixels;
得到手机屏幕宽度:
view plaincopy to clipboardprint?
dm.widthPixels;
dm.widthPixels;

得到以上手机屏幕的高度跟宽度后,即可以通过这两个值按照比例还设定程序布局中空间的大小。

6)获取CPU序列号

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
/**
* 获取CPU序列号
*
* @return CPU序列号(16位)
* 读取失败为"0000000000000000"
*/
public static String getCPUSerial() {
String str = "", strCPU = "", cpuAddress = "0000000000000000";
try {
//读取CPU信息
Process pp = Runtime.getRuntime().exec("cat /proc/cpuinfo");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
//查找CPU序列号
for (int i = 1; i
str = input.readLine();
if (str != null) {
//查找到序列号所在行
if (str.indexOf("Serial") > -1) {
//提取序列号
strCPU = str.substring(str.indexOf(":") + 1,
str.length());
//去空格
cpuAddress = strCPU.trim();
break;
}
}else{
//文件结尾
break;
}
}
} catch (IOException ex) {
//赋予默认值
ex.printStackTrace();
}
return cpuAddress;
}
/**
* 获取CPU序列号
*
* @return CPU序列号(16位)
* 读取失败为"0000000000000000"
*/
public static String getCPUSerial() {
String str = "", strCPU = "", cpuAddress = "0000000000000000";
try {
//读取CPU信息
Process pp = Runtime.getRuntime().exec("cat /proc/cpuinfo");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
//查找CPU序列号
for (int i = 1; i
str = input.readLine();
if (str != null) {
//查找到序列号所在行
if (str.indexOf("Serial") > -1) {
//提取序列号
strCPU = str.substring(str.indexOf(":") + 1,
str.length());
//去空格
cpuAddress = strCPU.trim();
break;
}
}else{
//文件结尾
break;
}
}
} catch (IOException ex) {
//赋予默认值
ex.printStackTrace();
}
return cpuAddress;
}

7)获取位置信息: locationManager
1.获取LocationManager对象
view plaincopy to clipboardprint?
String serviceString = Context.LOCATION_SERVICE;
LocationManager LocationManager = (LocationManager)getSystemService(serviceString);
String serviceString = Context.LOCATION_SERVICE;
LocationManager LocationManager = (LocationManager)getSystemService(serviceString);
2.选择定位方法
2.1 GPS_PROVIDER: GPS
2.2 NETWORK_PROVIDER: NETWORK
以network为例:
view plaincopy to clipboardprint?
String provider = LocationManager.NETWORK_PROVIDER
Location location = locationManager.getLaskKnownLocation(provider);
double lat = location.getLatitude();
double lng = location.getLongitude();
String provider = LocationManager.NETWORK_PROVIDER
Location location = locationManager.getLaskKnownLocation(provider);
double lat = location.getLatitude();
double lng = location.getLongitude();
8)当前时间和时区

System.currentTimeMillis()获取当前时间
时区:
TimeZone.getDefault();

 

 

 

 

 

 

 

 

 

 

原创粉丝点击