Android中获取设备的各种信息总结
一、屏幕分辨率
Displaydisplay=getWindowManager().getDefaultDisplay(); Pointsize=newPoint(); display.getSize(size); intwidth=size.x; intheight=size.y;
或者:
DisplayMetricsmetrics=newDisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); intwidth=metrics.widthPixels; intheight=metrics.heightPixels
上面的代码是要在能获取到Activity的情况下使用的,如果无法获取到Activity,则可以使用一下的代码:
WindowManagerwm=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE); Displaydisplay=wm.getDefaultDisplay(); Pointpoint=newPoint(); display.getSize(point); intwidth=point.x; intheight=point.y;
二、屏幕尺寸
DisplayMetricsdm=newDisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); intwidth=dm.widthPixels; intheight=dm.heightPixels; intdens=dm.densityDpi; doublewi=(double)width/(double)dens; doublehi=(double)height/(double)dens; doublex=Math.pow(wi,2); doubley=Math.pow(hi,2); doublescreenInches=Math.sqrt(x+y);
同样,上面的代码需要在能获取到Activity。
三、获取app名称
publicstaticStringgetAppName(Contextcontext){
StringappName="";
try{
PackageManagerpackageManager=context.getPackageManager();
ApplicationInfoapplicationInfo=packageManager.getApplicationInfo(context.getPackageName(),0);
appName=(String)packageManager.getApplicationLabel(applicationInfo);
}catch(PackageManager.NameNotFoundExceptione){
e.printStackTrace();
}
returnappName;
}
四、获取设备厂商和设备名称信息
//设备厂商 Stringbrand=Build.BRAND; //设备名称 Stringmodel=Build.MODEL;
获取DeviceID,SIM和IMSI
TelephonyManagertm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); StringdeviceId=tm.getDeviceId(); Stringsim=tm.getSimSerialNumber(); Stringimsi=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE).getSubscriberId();
注意需要在AndroidManifest中添加权限
<uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
五、获取网络状态
publicstaticStringgetAPNType(Contextcontext){
//结果返回值
StringnetType="nono_connect";
//获取手机所有连接管理对象
ConnectivityManagermanager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获取NetworkInfo对象
NetworkInfonetworkInfo=manager.getActiveNetworkInfo();
//NetworkInfo对象为空则代表没有网络
if(networkInfo==null){
returnnetType;
}
//否则NetworkInfo对象不为空则获取该networkInfo的类型
intnType=networkInfo.getType();
if(nType==ConnectivityManager.TYPE_WIFI){
//WIFI
netType="wifi";
}elseif(nType==ConnectivityManager.TYPE_MOBILE){
intnSubType=networkInfo.getSubtype();
TelephonyManagertelephonyManager=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
//4G
if(nSubType==TelephonyManager.NETWORK_TYPE_LTE
&&!telephonyManager.isNetworkRoaming()){
netType="4G";
}elseif(nSubType==TelephonyManager.NETWORK_TYPE_UMTS||nSubType==TelephonyManager.NETWORK_TYPE_HSDPA||nSubType==TelephonyManager.NETWORK_TYPE_EVDO_0&&!telephonyManager.isNetworkRoaming()){
netType="3G";
//2G移动和联通的2G为GPRS或EGDE,电信的2G为CDMA
}elseif(nSubType==TelephonyManager.NETWORK_TYPE_GPRS||nSubType==TelephonyManager.NETWORK_TYPE_EDGE||nSubType==TelephonyManager.NETWORK_TYPE_CDMA&&!telephonyManager.isNetworkRoaming()){
netType="2G";
}else{
netType="2G";
}
}
returnnetType;
}
六、判断设备是否root
网上有很多判断方法,但有些会在界面上弹窗提示获取权限,下面介绍一种无需弹窗判断设备是否root的方法:
/**判断手机是否root,不弹出root请求框<br/>*/
publicstaticbooleanisRoot(){
StringbinPath="/system/bin/su";
StringxBinPath="/system/xbin/su";
if(newFile(binPath).exists()&&isExecutable(binPath))
returntrue;
if(newFile(xBinPath).exists()&&isExecutable(xBinPath))
returntrue;
returnfalse;
}
privatestaticbooleanisExecutable(StringfilePath){
Processp=null;
try{
p=Runtime.getRuntime().exec("ls-l"+filePath);
//获取返回内容
BufferedReaderin=newBufferedReader(newInputStreamReader(
p.getInputStream()));
Stringstr=in.readLine();
if(str!=null&&str.length()>=4){
charflag=str.charAt(3);
if(flag=='s'||flag=='x')
returntrue;
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(p!=null){
p.destroy();
}
}
returnfalse;
}
七、总结
以上就是关于获取Android中设备各种信息的全部内容,这篇文章对大家开发AndroidApp具有一定参考借鉴价值,希望对大家能有所帮助,如果有疑问大家可以留言交流。