Android设备类
来源:互联网 发布:推荐les淘宝店 编辑:程序博客网 时间:2024/05/29 15:47
import java.io.IOException;import java.lang.reflect.Method;import android.content.Context;import android.content.Intent;import android.content.pm.PackageInfo;import android.content.pm.PackageManager.NameNotFoundException;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.net.Uri;import android.os.Build;import android.telephony.TelephonyManager;import android.text.TextUtils;import android.util.DisplayMetrics;import android.view.WindowManager;public class DeviceUtils {private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";public static void startDial(Context context, String phone) {try {// 平板无法打电话直接跳过context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone)));} catch (Exception e) {}}public static void startSms(Context context, String mobile, String msg) {Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+ mobile));intent.putExtra("sms_body", msg);context.startActivity(intent);}@SuppressWarnings("deprecation")public static final int getScreenWidth(final Context context) {WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);return manager.getDefaultDisplay().getWidth();}@SuppressWarnings("deprecation")public static final int getScreenHeight(final Context context) {WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);return manager.getDefaultDisplay().getHeight();}public static String getVersionName(Context context) {try {PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);return info.versionName;} catch (NameNotFoundException e) {e.printStackTrace();}return null;}public static int getVersionCode(Context context) {try {PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);return info.versionCode;} catch (NameNotFoundException e) {e.printStackTrace();}return 0;}public static final int SCREEN_240P = 240;public static final int SCREEN_360P = 360;public static final int SCREEN_480P = 480;public static final int SCREEN_720P = 720;public static final int SCREEN_1080P = 1080;public static final int SCREEN_1280P = 1280;/** * 获取屏幕分辨率 */public static int getDeviceScreen(Context context) {DisplayMetrics dm = null;dm = context.getResources().getDisplayMetrics();if (dm == null) {return -1;}int screenWidth = dm.widthPixels;if (screenWidth <= SCREEN_240P) {return SCREEN_240P;} else if (screenWidth > SCREEN_240P && screenWidth <= SCREEN_360P) {return SCREEN_360P;} else if (screenWidth > SCREEN_360P && screenWidth <= SCREEN_480P) {return SCREEN_480P;} else if (screenWidth > SCREEN_480P && screenWidth <= SCREEN_720P) {return SCREEN_720P;} else if (screenWidth > SCREEN_720P && screenWidth <= SCREEN_1080P) {return SCREEN_1080P;} else {return SCREEN_1280P;}}/** * 获取网络类型 */public static String getNetworkType(Context context) {NetworkInfo info = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();if (info == null) {return "无网络";}if (info.getType() == ConnectivityManager.TYPE_WIFI) {return "WIFI";}switch (info.getSubtype()) {case TelephonyManager.NETWORK_TYPE_UMTS:case TelephonyManager.NETWORK_TYPE_HSDPA:case TelephonyManager.NETWORK_TYPE_EVDO_0:case TelephonyManager.NETWORK_TYPE_EVDO_A:case TelephonyManager.NETWORK_TYPE_EVDO_B:return "3G";case TelephonyManager.NETWORK_TYPE_GPRS:case TelephonyManager.NETWORK_TYPE_EDGE:case TelephonyManager.NETWORK_TYPE_CDMA:return "2G";case TelephonyManager.NETWORK_TYPE_LTE:return "4G";default:return "";}}/** * 获取SIM卡运营商 */public static String getSimType(Context context) {String IMSI = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getSubscriberId();if (TextUtils.isEmpty(IMSI)) {return "无服务";}if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {return "中国移动";} else if (IMSI.startsWith("46001")) {return "中国联通";} else if (IMSI.startsWith("46003")) {return "中国电信";} else {return "未知运营商";}}/** * 获取IEMI */public static String getDeviceIEMI(Context context) {return String.valueOf(((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId());}/** * 获取设备型号 */public static String getDeviceModel() {return android.os.Build.MODEL;}/** * 获取系统版本名 */public static String getOSVersionName() {return android.os.Build.VERSION.RELEASE;}/** * 获取系统版本号 */public static int getOSVersionCode() {return android.os.Build.VERSION.SDK_INT;}public static boolean isMiui() {try {final BuildProperties prop = BuildProperties.newInstance();return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;} catch (final IOException e) {return false;}}public static boolean isFlyme4() {try {// Invoke Build.hasSmartBar()final Method method = Build.class.getMethod("hasSmartBar");return method != null;} catch (final Exception e) {return false;}}}
0 0
- Android设备类
- android-设备信息工具类
- android 设备信息工具类
- Android-设备工具类JDeviceUtil
- 利用DisplayMetrics类获取Android设备屏幕分辨率
- Android设备常用状态信息工具类
- 【Android】设备工具类(DeviceUtil.java)
- Android DeviceUtils-设备相关工具类
- Android设备屏幕问题
- Android设备信息获取
- android 获取设备型号
- Android 防止设备休眠
- android 获取设备信息
- Android 获取设备ID
- Android设备运行流程
- Android 的设备节点
- Android取设备ID
- Android定制新设备
- iOS urlencoded 转码解码
- effective c++-让自己习惯c++
- 树状数组的两种运用
- Java类与类之间的5种关系及uml表示
- SpringMVC学习笔记---- 一、SpringMVC基础入门,创建一个HelloWorld程序
- Android设备类
- [核格Hearken平台]核格平台连接Access数据库
- echartsX过多
- 读取properties里面配置的值
- SpringMvc
- PCL学习日记1
- js 时间操作
- linux shell数据重定向(输入重定向与输出重定向)详细分析
- C++与设计模式(9)——享元模式