Android获取设备信息
2016-05-04 15:04
501 查看
1.public void onCreate(Bundle savedInstanceState) { 2. super.onCreate(savedInstanceState); 3. setContentView(R.layout.main); 4. TextView textView = (TextView) findViewById(R.id.text); 5. textView.setText("Product Model: " + android.os.Build.MODEL + "," 6. + android.os.Build.VERSION.SDK + "," 7. + android.os.Build.VERSION.RELEASE); 8.}
注:android.os.Build.VERSION.RELEASE获取版本号
android.os.Build.MODEL 获取手机型号
一、CPU和内存的信息,可以直接从文件中读取; /** * 获得cpu名称 * * @return */ private String getCpuName() { FileReader fr = null; BufferedReader br = null; String text; try { fr = new FileReader("/proc/cpuinfo"); br = new BufferedReader(fr); text = br.readLine(); String[] array = text.split(":\\s+", 2); return array[1]; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fr.close(); br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } /** * 获得内存大小 * * @return */ private String getTotalMemory() { FileReader fr = null; BufferedReader br = null; String text; try { fr = new FileReader("/proc/meminfo"); br = new BufferedReader(fr, 8); text = br.readLine(); String[] array = text.split("\\s+"); //转换为GB显示 float memory = Float.valueOf(array[1]) / 1024 / 1024; //设置两位有效数字 DecimalFormat decimalFormat = new DecimalFormat("######0.00"); String p = decimalFormat.format(memory); return p; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fr.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }二、存储空间的获取,Android.os下的StatFs类主要用来获取文件系统的状态,能够获取sd卡的大小和剩余空间; /** * 获得存储大小 * * @return */ public String getAllAvaliableSize() { float allSize = getSdSize() + getRomTotalSize(); float memory = Float.valueOf(allSize) / 1024 / 1024 / 1024; DecimalFormat decimalFormat = new DecimalFormat("######0.00"); String p = decimalFormat.format(memory); return p; } /** * SD卡存储 * 兼容低版本 * @return */ public long getSdSize() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return blockSize * availableBlocks; } /** * 机身存储 * 兼容低版本 * @return */ public long getRomTotalSize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return blockSize * totalBlocks; } 这里要注意一下getBlockSize和getAvailableBlocks(),这两个方法已经不建议使用了,如果Android-18版本以上的建议使用getBlockSizeLong()和getBlockCountLong()。
3获取本机IP地址
public String getLocalHostIp() { String ipaddress = ""; try { Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); // 遍历所用的网络接口 while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip Enumeration<InetAddress> inet = nif.getInetAddresses(); // 遍历每一个接口绑定的所有ip while (inet.hasMoreElements()) { InetAddress ip = inet.nextElement(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip .getHostAddress())) { return ip.getHostAddress(); } } } } catch (SocketException e) { Log.e("feige", "获取本地ip地址失败"); e.printStackTrace(); } return ipaddress; }
相关文章推荐
- Android中Parcelable接口用法
- Android各种各样的Drawable-更新中
- android应用开发全程实录-你有多熟悉listview? .
- Android中队列的实现
- android使用自定控件实现城市列表展示并且实现当前城市定位
- ViewPage 翻页滑动
- DP、SP转PX (Android)
- ToolBar + DrawerLayout
- android客服端+eps8266+单片机+路由器之远程控制系统
- MVP设计模式思路
- 等比例宽高的ImageView
- Android使用外部字体
- Android旋转屏幕不销毁Activity
- Andriod 仿QQ顶部弹出式提示
- 根据宽高自动拉伸ImageView
- Android事件分发下
- PHP android ios相互兼容的AES加密算法
- Android自定义组合控件(二)
- Android加载器加载本地图片
- Android中的alertdialog