android 获取设备信息

  //手机号码
    public static String getLine1Number(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       if (tm == null) {
           return "";
       }
       return "" + tm.getLine1Number();
   }
    
  //deviceId
    public static String getDeviceId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       if (tm == null) {
           return "";
       }
       return "" + tm.getDeviceId();
   }
    
    //运营商名称
    public static String getNetworkOperatorName(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       if (tm == null) {
           return "";
       }
       return "" + tm.getNetworkOperatorName();
   }
    
  //sim卡序列号
    public static String getSimSerialNumber(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       if (tm == null) {
           return "";
       }
       return "" + tm.getSimSerialNumber();
   }
    
   //IMSI
    public static String getSubscriberId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       if (tm == null) {
           return "";
       }
       return "" + tm.getSubscriberId();
   }
    //sim卡所在国家
    public static String getNetworkCountryIso(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       if (tm == null) {
           return "";
       }
       return "" + tm.getNetworkCountryIso();
   }
    //运营商编号。
    public static String getNetworkOperator(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       if (tm == null) {
           return "";
       }
       return tm.getNetworkOperator();
   }    
    //android 获取当前手机型号
    public static String getPhoneModel(Context context) {
      Build bd = new Build();
         return  bd.MODEL;
   }    
   
    //android 获取当前手机品牌
    public static String getPhoneProduct(Context context) {
      Build bd = new Build();
         return  bd.PRODUCT;
   }    
    //android 获取屏幕分辩率
    public static String getMetrics(Context context) {
      DisplayMetrics dm = new DisplayMetrics();     
      int h = dm.heightPixels;
      int w = dm.widthPixels;
         return  h+ "*" +w;
   }    
    
   //android获取当前时区
    public static String getTimeZone(Context context) {
     TimeZone tz = TimeZone.getDefault();  
        String s = tz.getID();  
        System.out.println(s); 
        return s;
    }   
    
    //android获取当前日期时间
    public static String getDateAndTime(Context context) {
        SimpleDateFormat formatter = new SimpleDateFormat    ("yyyy-MM-dd HH:mm:ss");       
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间       
        String str = formatter.format(curDate);  
        return str;
    }   
    
    //获取手机系统语言 0中文简体 1其它
    public static String getLanguage(Context context) {
        Locale locale = context.getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        if (language.endsWith("zh"))
            return "0";
        else
            return "1";
    }
 /**
     * 获取网络类型
     */
    public static int getNetWorkType(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
            String type = networkInfo.getTypeName();

            if (type.equalsIgnoreCase("WIFI")) {
                return AVConstants.NETTYPE_WIFI;
            } else if (type.equalsIgnoreCase("MOBILE")) {
                NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                if (mobileInfo != null) {
                    switch (mobileInfo.getType()) {
                        case ConnectivityManager.TYPE_MOBILE:// 手机网络
                            switch (mobileInfo.getSubtype()) {
                                case TelephonyManager.NETWORK_TYPE_UMTS:
                                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                                case TelephonyManager.NETWORK_TYPE_HSDPA:
                                case TelephonyManager.NETWORK_TYPE_HSUPA:
                                case TelephonyManager.NETWORK_TYPE_HSPA:
                                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                                case TelephonyManager.NETWORK_TYPE_EHRPD:
                                case TelephonyManager.NETWORK_TYPE_HSPAP:
                                    return AVConstants.NETTYPE_3G;
                                case TelephonyManager.NETWORK_TYPE_CDMA:
                                case TelephonyManager.NETWORK_TYPE_GPRS:
                                case TelephonyManager.NETWORK_TYPE_EDGE:
                                case TelephonyManager.NETWORK_TYPE_1xRTT:
                                case TelephonyManager.NETWORK_TYPE_IDEN:
                                    return AVConstants.NETTYPE_2G;
                                case TelephonyManager.NETWORK_TYPE_LTE:
                                    return AVConstants.NETTYPE_4G;
                                default:
                                    return AVConstants.NETTYPE_NONE;
                            }
                    }
                }
            }
        }

        return AVConstants.NETTYPE_NONE;
    }

    /*
     * 网络连接是否可用
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                // 当前网络是连接的  
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    // 当前所连接的网络可用  
                    return true;
                }
            }
        }
        return false;
    }
    

Android 获取设备信息是一种常见的操作,通过获取设备信息,开发者可以了解到设备的各种属性,并根据这些信息进行相应的处理。 在 Android 平台上,获取设备信息可以通过调用系统提供的 API 实现。以下是一个获取设备信息的简单示例代码: ``` import android.os.Build; public class DeviceInfoDemo { public static void main(String[] args) { String deviceModel = Build.MODEL; // 获取设备型号 String deviceManufacturer = Build.MANUFACTURER; // 获取设备制造商 String deviceBrand = Build.BRAND; // 获取设备品牌 String deviceType = Build.TYPE; // 获取设备类型 String deviceOSVersion = Build.VERSION.RELEASE; // 获取操作系统版本号 System.out.println("设备型号:" + deviceModel); System.out.println("设备制造商:" + deviceManufacturer); System.out.println("设备品牌:" + deviceBrand); System.out.println("设备类型:" + deviceType); System.out.println("操作系统版本号:" + deviceOSVersion); } } ``` 以上代码使用了`Build`类提供的一些常用属性来获取设备信息,并将其打印输出。开发者可以根据实际需求获取更多的设备信息,例如 MAC 地址、IMEI 号等。 需要注意的是,为了使用`Build`类中的属性,需要在项目的`AndroidManifest.xml`文件中添加相应的权限声明。例如获取设备的 IMEI 号需要`android.permission.READ_PHONE_STATE`权限。 通过上述示例代码,开发者可以轻松获取 Android 设备的各种信息,这些信息对于开发、统计、调试等方面非常有用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

再见孙悟空_

你的鼓励将是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值