android 獲取設備信息

 

 

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 String BOARD The name of the underlying board, like "goldfish".(設備廠商)
public static final String BOOTLOADER The system bootloader version number.
public static final String BRAND The brand (e.g., carrier) the software is customized for, if any.
public static final String CPU_ABI The name of the instruction set (CPU type + ABI convention) of native code.
public static final String CPU_ABI2 The name of the second instruction set (CPU type + ABI convention) of native code.
public static final String DEVICE The name of the industrial design.
public static final String DISPLAY A build ID string meant for displaying to the user
public static final String FINGERPRINT A string that uniquely identifies this build.
public static final String HARDWARE The name of the hardware (from the kernel command line or /proc).
public static final String HOST  
public static final String ID Either a changelist number, or a label like "M4-rc20".
public static final String MANUFACTURER The manufacturer of the product/hardware.
public static final String MODEL The end-user-visible name for the end product.(設備名)
public static final String PRODUCT The name of the overall product.
public static final String RADIO This 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 String SERIAL A hardware serial number, if available.
public static final String TAGS Comma-separated tags describing the build, like "unsigned,debug".
public static final long TIME  
public static final String TYPE The type of build, like "user" or "eng".
public static final String USER

 

 

 

//獲取當前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();

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章