安卓中获取手机基本信息

package cn.wostore.android.woanalysis;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.provider.Settings;
import android.text.TextUtils;

import cn.wostore.android.util.NetworkUtil;
import cn.wostore.android.util.SystemUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**

  • 工具类。

  • @author chenxuliang

  • @version 1.0.0

  • @date 2017-11-7
    /
    public final class Utils {
    private Utils() {
    /
    cannot be instantiated */
    throw new UnsupportedOperationException(“cannot be instantiated”);
    }

    /**

    • 获取缓存路径
      */
      public static String getCachePath(Context context) {
      String cachePath = “”;
      //内部缓存
      File cacheFile = context.getCacheDir();
      if (cacheFile != null && cacheFile.exists()) {
      cachePath = cacheFile.getPath();
      }
      return cachePath;
      }

    /**

    • 获取保存crash日志的文件夹路径
      */
      public static String getSaveCrashInfoDir(Context context) {
      String saveDir = getCachePath(context) + File.separator + “WoAnalysis” + File.separator + “CrashInfo”;
      return saveDir;
      }

    /**

    • 获取保存打点日志的文件夹路径
      */
      public static String getSaveDottingInfoDir(Context context) {
      String saveDir = getCachePath(context) + File.separator + “WoAnalysis” + File.separator + “DottingInfo”;
      return saveDir;
      }

    /**

    • 根据时间戳产生文件名字
      */
      public static String generateFileName() {
      String fileName = System.currentTimeMillis() + “.data”;
      return fileName;
      }

    /**

    • 删除单个文件
      */
      public static boolean deleteFile(String path) {
      File file = new File(path);
      if (file.isFile() && file.exists()) {
      return file.delete();
      }
      return false;
      }

    /**

    • 获取app包名
      */
      public static String getAppPackageName(Context context) {
      String packageName = context.getPackageName();
      if (TextUtils.isEmpty(packageName)) {
      packageName = “”;
      }
      return packageName;
      }

    /**

    • 获取应用名
      */
      public static String getAppName(Context context) {
      String name = “”;
      PackageManager packageManager = context.getPackageManager();
      try {
      PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
      name = (String) packageManager.getApplicationLabel(packageInfo.applicationInfo);
      } catch (PackageManager.NameNotFoundException e) {
      e.printStackTrace();
      }
      if (TextUtils.isEmpty(name)) {
      name = “”;
      }
      return name;
      }

    /**

    • 获取app版本名字
      */
      public static String getAppVersionName(Context context) {
      String clientVersion = “”;
      PackageManager packageManager = context.getPackageManager();
      try {
      PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
      clientVersion = packageInfo.versionName;
      } catch (PackageManager.NameNotFoundException e) {
      e.printStackTrace();
      }
      return clientVersion == null ? “” : clientVersion;
      }

    /**

    • 获取app版本号
      */
      public static int getAppVersionCode(Context context) {
      int code = 0;
      PackageManager packageManager = context.getPackageManager();
      try {
      PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
      code = packageInfo.versionCode;
      } catch (PackageManager.NameNotFoundException e) {
      e.printStackTrace();
      }
      return code;
      }

    /**

    • 获取手机cpu架构
      */
      public static String getCpuABI() {
      return Build.CPU_ABI;
      }

    /**

    • 获取系统版本号,7.0等
      */
      public static String getOSVersion() {
      String str = Build.VERSION.RELEASE;
      if (TextUtils.isEmpty(str)) {
      str = “”;
      }
      return str;
      }

    /**

    • 获取Android API版本,24等
      */
      public static int getAndroidSDKVersion() {
      int num = Build.VERSION.SDK_INT;
      return num;
      }

    /**

    • 获取手机品牌,GIONEE等
      */
      public static String getBrand() {
      String str = Build.BRAND;
      if (TextUtils.isEmpty(str)) {
      str = “”;
      }
      return str;
      }

    /**

    • 手机型号,GN8002S等
      */
      public static String getModel() {
      String str = Build.MODEL;
      if (TextUtils.isEmpty(str)) {
      str = “”;
      }
      return str;
      }

    private static final long GB = 1073741824; // 1024 * 1024 * 1024
    private static final long MB = 1048576; // 1024 * 1024
    private static final long KB = 1024;

    /**

    • 文件大小获取
    • @param length 文件大小,单位KB
    • @return 文件大小字符串
      */
      public static String getFileSize(long length) {
      if (length >= GB) {
      return String.format("%.2f GB", length * 1.0 / GB);
      } else if (length >= MB) {
      return String.format("%.2f MB", length * 1.0 / MB);
      } else {
      return String.format("%.2f KB", length * 1.0 / KB);
      }
      }

    /**

    • 通过getprop获取系统信息
      */
      public static String getSystemProperty(String propName) {
      String result = “”;
      Process process = null;
      BufferedReader bufferedReader = null;
      try {
      process = Runtime.getRuntime().exec("getprop " + propName);
      } catch (IOException e) {
      e.printStackTrace();
      }
      try {
      InputStream inputStream = process.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
      bufferedReader = new BufferedReader(inputStreamReader, 8 * 1024);
      String info;
      while ((info = bufferedReader.readLine()) != null) {
      result += info;
      }
      } catch (IOException e) {
      e.printStackTrace();
      } finally {
      if (bufferedReader != null) {
      try {
      bufferedReader.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }
      return result;
      }

    /**

    • 获取网络连接状态
      */
      public static String getConnectStatus(Context context) {
      if (NetworkUtil.isConnected(context)) {
      if (NetworkUtil.isWifiConnected(context)) {
      return “WIFI”;
      } else {
      return “GPRS”;
      }
      } else {
      return “unconnected”;
      }
      }

    /**

    • 获取手机经纬度
    • 获取手机经纬度需要增加以下权限
    • @param context
    • @return
      */
      public static Location getLocation(Context context) {
      //获取地理位置管理器
      LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
      //获取所有可用的位置提供器
      List providers = locationManager.getProviders(true);
      String locationProvider;
      if (providers.contains(LocationManager.GPS_PROVIDER)) {
      //如果是GPS
      locationProvider = LocationManager.GPS_PROVIDER;
      } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
      //如果是Network
      locationProvider = LocationManager.NETWORK_PROVIDER;
      } else {
      return null;
      }
      //获取Location
      Location location = null;
      try {
      location = locationManager.getLastKnownLocation(locationProvider);
      } catch (Exception e) {
      e.printStackTrace();
      }
      return location;
      }

    /**

    • 获取总的内存 (RAM)
      */
      public static String getMemoryTotal(Context context) {
      ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
      ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
      am.getMemoryInfo(mi);
      return Utils.getFileSize(mi.totalMem);
      }

    /**

    • 获取可用的内存 (RAM)
      */
      public static String getMemoryFree(Context context) {
      ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
      ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
      am.getMemoryInfo(mi);
      return Utils.getFileSize(mi.availMem);
      }

    /**

    • 获得SD卡总大小
      */
      public static String getSDTotalSize() {
      File path = Environment.getExternalStorageDirectory();
      StatFs stat = new StatFs(path.getPath());
      long blockSize = stat.getBlockSize();
      long totalBlocks = stat.getBlockCount();
      return getFileSize(blockSize * totalBlocks);
      }

    /**

    • 获得sd卡剩余容量,即可用大小
      */
      public static String getSDAvailableSize() {
      File path = Environment.getExternalStorageDirectory();
      StatFs stat = new StatFs(path.getPath());
      long blockSize = stat.getBlockSize();
      long availableBlocks = stat.getAvailableBlocks();
      return getFileSize(blockSize * availableBlocks);
      }

    /**

    • 获得机身内存总大小
      */
      public static String getRomTotalSize() {
      File path = Environment.getDataDirectory();
      StatFs stat = new StatFs(path.getPath());
      long blockSize = stat.getBlockSize();
      long totalBlocks = stat.getBlockCount();
      return getFileSize(blockSize * totalBlocks);
      }

    /**

    • 获得机身可用内存
      */
      public static String getRomAvailableSize() {
      File path = Environment.getDataDirectory();
      StatFs stat = new StatFs(path.getPath());
      long blockSize = stat.getBlockSize();
      long availableBlocks = stat.getAvailableBlocks();
      return getFileSize(blockSize * availableBlocks);
      }

    /**

    • 获取当前线程名字
      */
      public static String getCurrentThreadName() {
      return Thread.currentThread().getName();
      }

    /**

    • 获取当前线程id
      */
      public static long getCurrentThreadId() {
      return Thread.currentThread().getId();
      }

    /**

    • 获取当前进程名
      */
      public static String getCurrentProcessName() {
      return getProcessName(android.os.Process.myPid());
      }

    /**

    • 获取进程号对应的进程名
    • @param pid 进程号
    • @return 进程名
      */
      public static String getProcessName(int pid) {
      BufferedReader reader = null;
      try {
      reader = new BufferedReader(new FileReader("/proc/" + pid + “/cmdline”));
      String processName = reader.readLine();
      if (!TextUtils.isEmpty(processName)) {
      processName = processName.trim();
      }
      return processName;
      } catch (Throwable throwable) {
      throwable.printStackTrace();
      } finally {
      try {
      if (reader != null) {
      reader.close();
      }
      } catch (IOException exception) {
      exception.printStackTrace();
      }
      }
      return “”;
      }

    /**

    • 获得一个UUID
    • @return String UUID
      */
      public static String getUUID() {
      String uuid = UUID.randomUUID().toString();
      //去掉“-”符号
      return uuid.replaceAll("-", “”);
      }

    /**

    • 获取随机数
      */
      public static int getRandomInt() {
      Random random = new Random();
      int num = random.nextInt(90000) + 10000;
      return num;
      }

    ///**
    // * 获取手机deviceId
    // /
    //public static String getDeviceId(Context context) {
    // String deviceId = “”;
    // TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    // if (telephonyManager != null) {
    // try {
    // deviceId = telephonyManager.getDeviceId();
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // }
    // return deviceId;
    //}
    //
    ///
    *
    // * 获取手机macAddress
    // */
    //public static String getMacAddress(Context context) {
    // String macAddress = “”;
    // WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    // if (wifi != null) {
    // WifiInfo info = wifi.getConnectionInfo();
    // if (info != null) {
    // macAddress = info.getMacAddress();
    // }
    // }
    // return macAddress;
    //}

    /**

    • 获取手机androidId
      */
      public static String getAndroidId(Context context) {
      String androidId = “”;
      try {
      androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
      } catch (Exception e) {
      e.printStackTrace();
      }
      return androidId;
      }

    /**

    • 获取手机SerialNumber
      */
      public static String getSerialNumber() {
      return Build.SERIAL;
      }

    /**

    • 获取手机唯一id
      */
      public static String getDeviceUniqueId(Context context) {
      String androidId = getAndroidId(context);
      String serialNumber = getSerialNumber();
      if (TextUtils.isEmpty(androidId)) {
      androidId = “”;
      }
      if (TextUtils.isEmpty(serialNumber)) {
      serialNumber = “”;
      }
      String uniqueId = androidId + serialNumber;
      if (TextUtils.isEmpty(uniqueId)) {
      uniqueId = SystemUtil.getImei(context);
      }
      if (TextUtils.isEmpty(uniqueId)) {
      uniqueId = “”;
      }
      return uniqueId;
      }

    /**

    • CORE-VER
    • 内核版本
    • return String
      */

    public static String getLinuxCore_Ver() {
    Process process = null;
    String kernelVersion = “”;
    try {
    process = Runtime.getRuntime().exec(“cat /proc/version”);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

     // get the output line
     InputStream outs = process.getInputStream();
     InputStreamReader isrout = new InputStreamReader(outs);
     BufferedReader brout = new BufferedReader(isrout, 8 * 1024);
    
    
     String result = "";
     String line;
     try {
         while ((line = brout.readLine()) != null) {
             result += line;
         }
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     } finally {
         try {
             if (brout != null) {
                 brout.close();
             }
             if (isrout != null) {
                 isrout.close();
             }
             if (outs != null) {
                 outs.close();
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    
     try {
         if (result != "") {
             String Keyword = "version ";
             int index = result.indexOf(Keyword);
             line = result.substring(index + Keyword.length());
             index = line.indexOf(" ");
             kernelVersion = line.substring(0, index);
         }
     } catch (IndexOutOfBoundsException e) {
         e.printStackTrace();
     }
     return kernelVersion;
    

    }
    }

猜你喜欢

转载自blog.csdn.net/weixin_45361581/article/details/109647348