您的位置:首页 > 移动开发 > Android开发

Android获取设备或应用基本信息

2015-10-08 11:52 435 查看
一、Build 获取设备信息android.os.Build: 获取Android手机的一些硬件参数,常见的属性常量:Build.MODEL :设备名Build.VERSION.SDK :sdk版本号Build.BOARD :设备参数Build.FINGERPRINT :硬件名称Build.PRODUCT:手机制造商常见使用:if (android.os.Build.VERSION.SDK_INT> 10) {//要进行的操作} else{//要进行的操作}二、获取屏幕宽高
DisplayMetrics dm = new DisplayMetrics();
//获取屏幕信息3        getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeigh = dm.heightPixels;
先获取到手机的宽和高 windmanader smsmanager telephoneManagerWindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);int screenWidth = wm.getDefaultDisplay().getWidth(); // 获取到屏幕的宽int screenHeight = wm.getDefaultDisplay().getHeight(); //屏幕的高度三、获取系统API版本Build.VERSION.SDK_INT判断手机系统的api版本号Demo:如果api在16以上使用setBackground的方法;
if (Build.VERSION.SDK_INT >= 16)
view.setBackground(...);
else
view.setBackgroundDrawable(...);
Build.VERSION_CODES.JELLY_BEAN  对应版本:16 Android 4.1
Build.VERSION_CODES.HONEYCOMB  对应版本:11 Android 3.0
四、versionName 和 VersionCode
versionNamepublic static String getVersion(Context context)//获取版本号{try {PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);return pi.versionName;} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();return context.getString(R.string.version_unknown);}}public static int getVersionCode(Context context)//获取版本号(内部识别号){try {PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);return pi.versionCode;} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();return 0;}}public static int getVersionCode(Context context)//获取版本号(内部识别号){try {PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);return pi.versionCode;} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();return 0;}} 五、SD卡信息#1 获取SD卡状态    String    Environment.getExternalStorageState();    Environment.MEDIA_MOUNTED :被挂载了#2 获取SD卡路径    Environment.getExternalStorageDirectory();获取SD的大小及可用空间//获得sd卡的目录对象File file = Environment.getExternalStorageDirectory();//获得sd卡总空间的大小long total =  file.getTotalSpace();//转换数据大小的数据单位String totalSize = Formatter.formatFileSize(this, total);//获得sd卡剩余空间的大小long usable = file.getUsableSpace();String usableSize = Formatter.formatFileSize(this, usable);tv.setText(usableSize+"/"+totalSize);将数据存储到SD卡上先要判断SD卡的状态;//SD卡可用Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())Environment.MEDIA_MOUNTED:SD卡被挂载到手机上了Environment.getExternalStorageState():获取SD卡的状态.获取SD的大小及可用空间     File file = Environment.getExternalStorageDirectory();   //获取sd卡总大小  long totalSpace = file.getTotalSpace();    //获取sd卡可用空间  long usableSpace = file.getUsableSpace();   String totalSize = Formatter.formatFileSize(contexts, totalSpace);  String usableSize = Formatter.formatFileSize(contexts, usableSpace);   //把转换后的数据 显示到 Textview上  tv_total.setText(totalSize);  tv_useable.setText(usableSize);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: