DeviceUtils.kt 25.3 KB
Newer Older
1
package com.ydl.ydlcommon.utils
konghaorui committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

import android.annotation.TargetApi
import android.app.Activity
import android.app.Dialog
import android.content.*
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.graphics.Point
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.os.PowerManager
import android.telephony.TelephonyManager
import android.text.TextUtils
import android.util.DisplayMetrics
import android.util.Log
import android.view.Display
import android.view.View
import android.view.ViewConfiguration
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager

import java.io.File
import java.lang.reflect.Field
import java.text.NumberFormat
/**
 * Created by haorui on 2019-08-22 .
 * Des: 设备信息工具类
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
class DeviceUtils private constructor() {

    init {
        throw IllegalStateException("you can't instantiate me!")
    }

    companion object {
        // 手机网络类型
        val NETTYPE_WIFI = 0x01
        val NETTYPE_CMWAP = 0x02
        val NETTYPE_CMNET = 0x03
        var GTE_HC: Boolean = false
        var GTE_ICS: Boolean = false
        var PRE_HC: Boolean = false
        private var _hasBigScreen: Boolean? = null
        private var _hasCamera: Boolean? = null
        private var _isTablet: Boolean? = null
        private var _loadFactor: Int? = null
        var displayDensity = 0.0f

        init {
            GTE_ICS = Build.VERSION.SDK_INT >= 14
            GTE_HC = Build.VERSION.SDK_INT >= 11
            PRE_HC = Build.VERSION.SDK_INT < 11
        }

        /**
         * dp转px
         *
         * @param context
         * @param dp
         * @return
         */
        fun dpToPixel(context: Context, dp: Float): Float {
            return dp * (getDisplayMetrics(context).densityDpi / 160f)
        }

        /**
         * px转dp
         *
         * @param context
         * @param f
         * @return
         */
        fun pixelsToDp(context: Context, f: Float): Float {
            return f / (getDisplayMetrics(context).densityDpi / 160f)
        }

        fun getDefaultLoadFactor(context: Context): Int {
            if (_loadFactor == null) {
                val integer = Integer.valueOf(
                    0xf and context
                        .resources.configuration.screenLayout
                )
                _loadFactor = integer
                _loadFactor = Integer.valueOf(Math.max(integer.toInt(), 1))
            }
            return _loadFactor!!.toInt()
        }

        fun getDensity(context: Context): Float {
            if (displayDensity.toDouble() == 0.0)
96 97 98
                displayDensity = getDisplayMetrics(
                    context
                ).density
konghaorui committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
            return displayDensity
        }

        fun getDisplayMetrics(context: Context): DisplayMetrics {
            val displaymetrics = DisplayMetrics()
            (context.getSystemService(
                Context.WINDOW_SERVICE
            ) as WindowManager).defaultDisplay.getMetrics(
                displaymetrics
            )
            return displaymetrics
        }

        /**
         * 屏幕高度
         *
         * @param context
         * @return
         */
        fun getScreenHeight(context: Context): Float {
            return getDisplayMetrics(context).heightPixels.toFloat()
        }

        /**
         * 屏幕宽度
         *
         * @param context
         * @return
         */
        fun getScreenWidth(context: Context): Float {
            return getDisplayMetrics(context).widthPixels.toFloat()
        }

        /**
         * 获取activity尺寸
         *
         * @param activity
         * @return
         */
        fun getRealScreenSize(activity: Activity): IntArray {
            val size = IntArray(2)
            var screenWidth = 0
            var screenHeight = 0
            val w = activity.windowManager
            val d = w.defaultDisplay
            val metrics = DisplayMetrics()
            d.getMetrics(metrics)
            // since SDK_INT = 1;
            screenWidth = metrics.widthPixels
            screenHeight = metrics.heightPixels
            // includes window decorations (statusbar bar/menu bar)
            if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
                try {
                    screenWidth = Display::class.java.getMethod("getRawWidth")
                        .invoke(d) as Int
                    screenHeight = Display::class.java
                        .getMethod("getRawHeight").invoke(d) as Int
                } catch (ignored: Exception) {
                }

            // includes window decorations (statusbar bar/menu bar)
            if (Build.VERSION.SDK_INT >= 17)
                try {
                    val realSize = Point()
                    Display::class.java.getMethod("getRealSize", Point::class.java).invoke(
                        d,
                        realSize
                    )
                    screenWidth = realSize.x
                    screenHeight = realSize.y
                } catch (ignored: Exception) {
                }

            size[0] = screenWidth
            size[1] = screenHeight
            return size
        }

        /**
         * 获取状态栏高度
         *
         * @param context
         * @return
         */
        fun getStatusBarHeight(context: Context): Int {
            var c: Class<*>? = null
            var obj: Any? = null
            var field: Field? = null
            var x = 0
            try {
                c = Class.forName("com.android.internal.R\$dimen")
                obj = c!!.newInstance()
                field = c.getField("status_bar_height")
                x = Integer.parseInt(field!!.get(obj).toString())
                return context.resources
                    .getDimensionPixelSize(x)
            } catch (e: Exception) {
                e.printStackTrace()
            }

            return 0
        }

        fun hasBigScreen(context: Context): Boolean {
            var flag = true
            if (_hasBigScreen == null) {
                val flag1: Boolean
                if (0xf and context.resources
                        .configuration.screenLayout >= 3
                )
                    flag1 = flag
                else
                    flag1 = false
                val boolean1 = java.lang.Boolean.valueOf(flag1)
                _hasBigScreen = boolean1
                if (!boolean1) {
                    if (getDensity(context) <= 1.5f)
                        flag = false
                    _hasBigScreen = java.lang.Boolean.valueOf(flag)
                }
            }
            return _hasBigScreen!!
        }

        /**
         * 设备是否有相机
         *
         * @param context
         * @return
         */
        fun hasCamera(context: Context): Boolean {
            if (_hasCamera == null) {
                val pckMgr = context
                    .packageManager
                val flag = pckMgr
                    .hasSystemFeature("android.hardware.camera.front")
                val flag1 = pckMgr.hasSystemFeature("android.hardware.camera")
                val flag2: Boolean
                if (flag || flag1)
                    flag2 = true
                else
                    flag2 = false
                _hasCamera = java.lang.Boolean.valueOf(flag2)
            }
            return _hasCamera!!
        }

        /**
         * 设备是否有实体菜单
         *
         * @param context
         * @return
         */
        fun hasHardwareMenuKey(context: Context): Boolean {
            var flag = false
            if (PRE_HC)
                flag = true
            else if (GTE_ICS) {
                flag = ViewConfiguration.get(context).hasPermanentMenuKey()
            } else
                flag = false
            return flag
        }

        /**
         * 当前是否有网
         *
         * @param context
         * @return
         */
        fun hasInternet(context: Context): Boolean {
            val flag: Boolean
            val manager =
                context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            if (manager != null && manager.activeNetworkInfo != null)
                flag = true
            else
                flag = false
            return flag
        }

        /**
         * 当前的包是否存在
         *
         * @param context
         * @param pckName
         * @return
         */
        fun isPackageExist(context: Context, pckName: String): Boolean {
            try {
                val pckInfo = context.packageManager
                    .getPackageInfo(pckName, 0)
                if (pckInfo != null)
                    return true
            } catch (e: PackageManager.NameNotFoundException) {
                Log.e("TDvice", e.message)
            }

            return false
        }

        fun hideAnimatedView(view: View?) {
            if (PRE_HC && view != null)
                view.setPadding(view.width, 0, 0, 0)
        }

        /**
         * 隐藏软键盘
         *
         * @param context
         * @param view
         */
        fun hideSoftKeyboard(context: Context, view: View?) {
            if (view == null)
                return
            val inputMethodManager = context.getSystemService(
                Context.INPUT_METHOD_SERVICE
            ) as InputMethodManager
            if (inputMethodManager.isActive)
                inputMethodManager.hideSoftInputFromWindow(
                    view.windowToken, 0
                )
        }

        /**
         * 是否是横屏
         *
         * @param context
         * @return
         */
        fun isLandscape(context: Context): Boolean {
            val flag: Boolean
            if (context.resources.configuration.orientation == 2)
                flag = true
            else
                flag = false
            return flag
        }

        /**
         * 是否是竖屏
         *
         * @param context
         * @return
         */
        fun isPortrait(context: Context): Boolean {
            var flag = true
            if (context.resources.configuration.orientation != 1)
                flag = false
            return flag
        }

        fun isTablet(context: Context): Boolean {
            if (_isTablet == null) {
                val flag: Boolean
                if (0xf and context.resources
                        .configuration.screenLayout >= 3
                )
                    flag = true
                else
                    flag = false
                _isTablet = java.lang.Boolean.valueOf(flag)
            }
            return _isTablet!!
        }

        fun showAnimatedView(view: View?) {
            if (PRE_HC && view != null)
                view.setPadding(0, 0, 0, 0)
        }

        fun showSoftKeyboard(dialog: Dialog) {
            dialog.window!!.setSoftInputMode(4)
        }

        fun showSoftKeyboard(context: Context, view: View) {
            (context.getSystemService(
                Context.INPUT_METHOD_SERVICE
            ) as InputMethodManager).showSoftInput(
                view,
                InputMethodManager.SHOW_FORCED
            )
        }

        fun toogleSoftKeyboard(context: Context, view: View) {
            (context.getSystemService(
                Context.INPUT_METHOD_SERVICE
            ) as InputMethodManager).toggleSoftInput(
                0,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        }

        val isSdcardReady: Boolean
            get() = Environment.MEDIA_MOUNTED == Environment
                .getExternalStorageState()

        fun getCurCountryLan(context: Context): String {
            return (context.resources.configuration.locale
                .language
                    + "-"
                    + context.resources.configuration.locale
                .country)
        }

        fun isZhCN(context: Context): Boolean {
            val lang = context.resources
                .configuration.locale.country
            return if (lang.equals("CN", ignoreCase = true)) {
                true
            } else false
        }

        fun percent(p1: Double, p2: Double): String {
            val str: String
            val p3 = p1 / p2
            val nf = NumberFormat.getPercentInstance()
            nf.minimumFractionDigits = 2
            str = nf.format(p3)
            return str
        }

        fun percent2(p1: Double, p2: Double): String {
            val str: String
            val p3 = p1 / p2
            val nf = NumberFormat.getPercentInstance()
            nf.minimumFractionDigits = 0
            str = nf.format(p3)
            return str
        }

        fun isHaveMarket(context: Context): Boolean {
            val intent = Intent()
            intent.action = "android.intent.action.MAIN"
            intent.addCategory("android.intent.category.APP_MARKET")
            val pm = context.packageManager
            val infos = pm.queryIntentActivities(intent, 0)
            return infos.size > 0
        }

        fun setFullScreen(activity: Activity) {
            val params = activity.window
                .attributes
            params.flags = params.flags or WindowManager.LayoutParams.FLAG_FULLSCREEN
            activity.window.attributes = params
            activity.window.addFlags(
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
            )
        }

        fun cancelFullScreen(activity: Activity) {
            val params = activity.window
                .attributes
            params.flags = params.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN.inv()
            activity.window.attributes = params
            activity.window.clearFlags(
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
            )
        }

        fun getPackageInfo(context: Context, pckName: String): PackageInfo? {
            try {
                return context.packageManager
                    .getPackageInfo(pckName, 0)
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }

            return null
        }

        /**
         * 获取版本号
         *
         * @param context
         * @return
         */
        fun getVersionCode(context: Context): Int {
            var versionCode = 0
            try {
                versionCode = context.packageManager
                    .getPackageInfo(
                        context.packageName,
                        0
                    ).versionCode
            } catch (ex: PackageManager.NameNotFoundException) {
                versionCode = 0
            }

            return versionCode
        }

        /**
         * 获取指定包名应用的版本号
         *
         * @param context
         * @param packageName
         * @return
         */
        fun getVersionCode(context: Context, packageName: String): Int {
            var versionCode = 0
            try {
                versionCode = context.packageManager
                    .getPackageInfo(packageName, 0).versionCode
            } catch (ex: PackageManager.NameNotFoundException) {
                versionCode = 0
            }

            return versionCode
        }

        /**
         * 获取版本名
         *
         * @param context
         * @return
         */
        fun getVersionName(context: Context): String {
            var name = ""
            try {
                name = context.packageManager
                    .getPackageInfo(
                        context.packageName,
                        0
                    ).versionName
            } catch (ex: PackageManager.NameNotFoundException) {
                name = ""
            }

            return name
        }

        fun isScreenOn(context: Context): Boolean {
            val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager
            return pm.isScreenOn
        }

        /**
         * 安装应用
         *
         * @param context
         * @param file
         */
        fun installAPK(context: Context, file: File?) {
            if (file == null || !file.exists())
                return
            val intent = Intent()
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            intent.action = Intent.ACTION_VIEW
            intent.setDataAndType(
                Uri.fromFile(file),
                "application/vnd.android.package-archive"
            )
            context.startActivity(intent)
        }

        fun getInstallApkIntent(file: File): Intent {
            val intent = Intent()
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            intent.action = Intent.ACTION_VIEW
            intent.setDataAndType(
                Uri.fromFile(file),
                "application/vnd.android.package-archive"
            )
            return intent
        }

        /**
         * 拨打电话
         *
         * @param context
         * @param number
         */
        fun openDial(context: Context, number: String) {
            val uri = Uri.parse("tel:$number")
            val it = Intent(Intent.ACTION_DIAL, uri)
            context.startActivity(it)
        }

        fun openSMS(context: Context, smsBody: String, tel: String) {
            val uri = Uri.parse("smsto:$tel")
            val it = Intent(Intent.ACTION_SENDTO, uri)
            it.putExtra("sms_body", smsBody)
            context.startActivity(it)
        }

        fun openDail(context: Context) {
            val intent = Intent(Intent.ACTION_DIAL)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)
        }

        fun openSendMsg(context: Context) {
            val uri = Uri.parse("smsto:")
            val intent = Intent(Intent.ACTION_SENDTO, uri)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)
        }

        fun openCamera(context: Context) {
            val intent = Intent() // 调用照相机
            intent.action = "android.media.action.STILL_IMAGE_CAMERA"
            intent.flags = 0x34c40000
            context.startActivity(intent)
        }

        fun getIMEI(context: Context): String {
            val tel = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            return tel.deviceId
        }

        val phoneType: String
            get() = Build.MODEL

        fun openApp(context: Context, packageName: String) {
            var mainIntent = context.packageManager
                .getLaunchIntentForPackage(packageName)
            if (mainIntent == null) {
                mainIntent = Intent(packageName)
            } else {
            }
            context.startActivity(mainIntent)
        }

        fun openAppActivity(
            context: Context, packageName: String,
            activityName: String
        ): Boolean {
            val intent = Intent(Intent.ACTION_MAIN)
            intent.addCategory(Intent.CATEGORY_LAUNCHER)
            val cn = ComponentName(packageName, activityName)
            intent.component = cn
            try {
                context.startActivity(intent)
                return true
            } catch (e: Exception) {
                return false
            }

        }

        /**
         * wifi是否开启
         *
         * @param context
         * @return
         */
        fun isWifiOpen(context: Context): Boolean {
            var isWifiConnect = false
            val cm = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            // check the networkInfos numbers
            val networkInfos = cm.allNetworkInfo
            for (i in networkInfos.indices) {
                if (networkInfos[i].state == NetworkInfo.State.CONNECTED) {
                    if (networkInfos[i].type == ConnectivityManager.TYPE_MOBILE) {
                        isWifiConnect = false
                    }
                    if (networkInfos[i].type == ConnectivityManager.TYPE_WIFI) {
                        isWifiConnect = true
                    }
                }
            }
            return isWifiConnect
        }

        /**
         * 卸载软件
         *
         * @param context
         * @param packageName
         */
        fun uninstallApk(context: Context, packageName: String) {
            if (isPackageExist(context, packageName)) {
                val packageURI = Uri.parse("package:$packageName")
                val uninstallIntent = Intent(
                    Intent.ACTION_DELETE,
                    packageURI
                )
                context.startActivity(uninstallIntent)
            }
        }

        fun copyTextToBoard(context: Context, string: String) {
            if (TextUtils.isEmpty(string))
                return
            val clip = context
                .getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            clip.text = string
        }

        /**
         * 发送邮件
         *
         * @param context
         * @param subject 主题
         * @param content 内容
         * @param emails  邮件地址
         */
        fun sendEmail(
            context: Context, subject: String,
            content: String, vararg emails: String
        ) {
            try {
                val intent = Intent(Intent.ACTION_SEND)
                // 模拟器
                // intent.setType("text/plain");
                intent.type = "message/rfc822" // 真机
                intent.putExtra(Intent.EXTRA_EMAIL, emails)
                intent.putExtra(Intent.EXTRA_SUBJECT, subject)
                intent.putExtra(Intent.EXTRA_TEXT, content)
                context.startActivity(intent)
            } catch (e: ActivityNotFoundException) {
                e.printStackTrace()
            }

        }

        fun getStatuBarHeight(context: Context): Int {
            var c: Class<*>? = null
            var obj: Any? = null
            var field: Field? = null
            var x = 0
            var sbar = 38// 默认为38,貌似大部分是这样的
            try {
                c = Class.forName("com.android.internal.R\$dimen")
                obj = c!!.newInstance()
                field = c.getField("status_bar_height")
                x = Integer.parseInt(field!!.get(obj).toString())
                sbar = context.resources
                    .getDimensionPixelSize(x)

            } catch (e1: Exception) {
                e1.printStackTrace()
            }

            return sbar
        }

        fun hasStatusBar(activity: Activity): Boolean {
            val attrs = activity.window.attributes
            return if (attrs.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
                false
            } else {
                true
            }
        }

        /**
         * 调用系统安装了的应用分享
         *
         * @param context
         * @param title
         * @param url
         */
        fun showSystemShareOption(
            context: Activity,
            title: String, url: String
        ) {
            val intent = Intent(Intent.ACTION_SEND)
            intent.type = "text/plain"
            intent.putExtra(Intent.EXTRA_SUBJECT, "分享:$title")
            intent.putExtra(Intent.EXTRA_TEXT, "$title $url")
            context.startActivity(Intent.createChooser(intent, "选择分享"))
        }

        /**
         * 获取当前网络类型
         *
         * @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
         */
        fun getNetworkType(context: Context): Int {
            var netType = 0
            val connectivityManager =
                context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val networkInfo = connectivityManager.activeNetworkInfo ?: return netType
            val nType = networkInfo.type
            if (nType == ConnectivityManager.TYPE_MOBILE) {
                val extraInfo = networkInfo.extraInfo
                if (extraInfo != null && !extraInfo.isEmpty()) {
                    if (extraInfo.equals("cmnet", ignoreCase = true)) {
                        netType = NETTYPE_CMNET
                    } else {
                        netType = NETTYPE_CMWAP
                    }
                }
            } else if (nType == ConnectivityManager.TYPE_WIFI) {
                netType = NETTYPE_WIFI
            }
            return netType
        }

        fun netIsConnected(context: Context): Boolean {
            val connectMgr =
                context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            //手机网络连接状态
            val mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            //WIFI连接状态
            val wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            return if (!mobNetInfo.isConnected && !wifiNetInfo.isConnected) {
                //当前无可用的网络
                false
            } else true
        }

        /**
         * 判断是否存在sd卡
         *
         * @return
         */
        val isExitsSdcard: Boolean
            get() = if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED)
                true
            else
                false
    }
}