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

来源:互联网 发布:做微商网络爆款赚钱吗 编辑:程序博客网 时间:2024/05/24 05:15

一、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    telephoneManager
   WindowManager 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
versionName
  1. public static String getVersion(Context context)//获取版本号  
  2.     {  
  3.         try {  
  4.             PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);  
  5.             return pi.versionName;  
  6.         } catch (NameNotFoundException e) {  
  7.             // TODO Auto-generated catch block  
  8.             e.printStackTrace();  
  9.             return context.getString(R.string.version_unknown);  
  10.         }  
  11.     } 


  1. public static int getVersionCode(Context context)//获取版本号(内部识别号)  
  2. {  
  3.     try {  
  4.         PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);  
  5.         return pi.versionCode;  
  6.     } catch (NameNotFoundException e) {  
  7.         // TODO Auto-generated catch block  
  8.         e.printStackTrace();  
  9.         return 0;  
  10.     }  
  11. }  

  1. public static int getVersionCode(Context context)//获取版本号(内部识别号)  
  2. {  
  3.     try {  
  4.         PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);  
  5.         return pi.versionCode;  
  6.     } catch (NameNotFoundException e) {  
  7.         // TODO Auto-generated catch block  
  8.         e.printStackTrace();  
  9.         return 0;  
  10.     }  
  11. }  
五、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);

0 0