欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中获取设备的各种信息总结

程序员文章站 2024-03-05 23:22:31
一、屏幕分辨率 display display = getwindowmanager().getdefaultdisplay(); point size = n...

一、屏幕分辨率

display display = getwindowmanager().getdefaultdisplay();
point size = new point();
display.getsize(size);
int width = size.x;
int height = size.y;

或者:

displaymetrics metrics = new displaymetrics();
getwindowmanager().getdefaultdisplay().getmetrics(metrics);
int width = metrics.widthpixels;
int height = metrics.heightpixels

上面的代码是要在能获取到activity的情况下使用的,如果无法获取到activity,则可以使用一下的代码:

windowmanager wm = (windowmanager)context.getsystemservice(context.window_service);
display display = wm.getdefaultdisplay();
point point = new point();
display.getsize(point);
int width = point.x;
int height = point.y;

二、屏幕尺寸

displaymetrics dm = new displaymetrics();
getwindowmanager().getdefaultdisplay().getmetrics(dm);
int width=dm.widthpixels;
int height=dm.heightpixels;
int dens=dm.densitydpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = math.pow(wi,2);
double y = math.pow(hi,2);
double screeninches = math.sqrt(x+y);

同样,上面的代码需要在能获取到activity。

三、获取app名称

public static string getappname(context context) {
  string appname = "";
  try {
    packagemanager packagemanager = context.getpackagemanager();
    applicationinfo applicationinfo = packagemanager.getapplicationinfo(context.getpackagename(), 0);
    appname = (string) packagemanager.getapplicationlabel(applicationinfo);
  } catch (packagemanager.namenotfoundexception e) {
    e.printstacktrace();
  }
  return appname;
}

四、获取设备厂商和设备名称信息

// 设备厂商
string brand = build.brand;
// 设备名称
string model = build.model;

获取deviceid,sim和imsi

telephonymanager tm = (telephonymanager) context.getsystemservice(context.telephony_service);
string deviceid = tm.getdeviceid();
string sim = tm.getsimserialnumber();
string imsi = (telephonymanager) getsystemservice(context.telephony_service).getsubscriberid();

注意需要在androidmanifest中添加权限

<uses-permission android:name="android.permission.read_phone_state"/>

五、获取网络状态

public static string getapntype(context context) {
  //结果返回值
  string nettype = "nono_connect";
  //获取手机所有连接管理对象
  connectivitymanager manager = (connectivitymanager) context.getsystemservice(context.connectivity_service);
  //获取networkinfo对象
  networkinfo networkinfo = manager.getactivenetworkinfo();
  //networkinfo对象为空 则代表没有网络
  if (networkinfo == null) {
    return nettype;
  }
  //否则 networkinfo对象不为空 则获取该networkinfo的类型
  int ntype = networkinfo.gettype();
  if (ntype == connectivitymanager.type_wifi) {
    //wifi
    nettype = "wifi";
  } else if (ntype == connectivitymanager.type_mobile) {
    int nsubtype = networkinfo.getsubtype();
    telephonymanager telephonymanager = (telephonymanager) context.getsystemservice(context.telephony_service);
    //4g
    if (nsubtype == telephonymanager.network_type_lte
        && !telephonymanager.isnetworkroaming()) {
      nettype = "4g";
    } else if (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
    } else if (nsubtype == telephonymanager.network_type_gprs || nsubtype == telephonymanager.network_type_edge || nsubtype == telephonymanager.network_type_cdma && !telephonymanager.isnetworkroaming()) {
      nettype = "2g";
    } else {
      nettype = "2g";
    }
  }
  return nettype;
}

六、判断设备是否root

网上有很多判断方法,但有些会在界面上弹窗提示获取权限,下面介绍一种无需弹窗判断设备是否root的方法:

/** 判断手机是否root,不弹出root请求框<br/> */
  public static boolean isroot() {
    string binpath = "/system/bin/su";
    string xbinpath = "/system/xbin/su";
    if (new file(binpath).exists() && isexecutable(binpath))
      return true;
    if (new file(xbinpath).exists() && isexecutable(xbinpath))
      return true;
    return false;
  }

  private static boolean isexecutable(string filepath) {
    process p = null;
    try {
      p = runtime.getruntime().exec("ls -l " + filepath);
      // 获取返回内容
      bufferedreader in = new bufferedreader(new inputstreamreader(
          p.getinputstream()));
      string str = in.readline();
      if (str != null && str.length() >= 4) {
        char flag = str.charat(3);
        if (flag == 's' || flag == 'x')
          return true;
      }
    } catch (ioexception e) {
      e.printstacktrace();
    } finally {
      if (p != null) {
        p.destroy();
      }
    }
    return false;
  }

七、总结

以上就是关于获取android中设备各种信息的全部内容,这篇文章对大家开发android app具有一定参考借鉴价值,希望对大家能有所帮助,如果有疑问大家可以留言交流。