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
原创粉丝点击