开启飞行模式并出现Recevier错误后,应用崩溃

时间:2019-07-02 05:41:53

标签: android broadcastreceiver android-wifi airplane android-connectivitymanager

当我在应用程序中打开飞行模式时,它就会死掉。

这是错误消息:

java.lang.RuntimeException: Unable to instantiate receiver receiver.NetworkChangeReceiver: java.lang.ClassNotFoundException: Didn't find class "receiver.NetworkChangeReceiver" on path: DexPathList[[zip file "/data/app/com.example-on9FG2ds8YTS_e65wNBc-Q==/base.apk"],nativeLibraryDirectories=[/data/app/com.example-on9FG2ds8YTS_e65wNBc-Q==/lib/arm64, /system/lib64, /product/lib64]]
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3842)
        at android.app.ActivityThread.access$2000(ActivityThread.java:267)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1974)
        at android.os.Handler.dispatchMessage(Handler.java:109)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:7470)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)

这是NetworkChangeReceiver

class NetworkChangeReceiver : BroadcastReceiver() {
    var dialog: Dialog? = null

    override fun onReceive(context: Context, intent: Intent) {
        if (!isAirplaneModeOn(context)) {
            val isConnected = NetworkUtil.getConnectivityStatusString(context)
            if (!isConnected) {
                showConnectionFailDialog(context)
            }
        }else{
            showConnectionFailDialog(context)
        }
    }

    private fun showConnectionFailDialog(context: Context) {
        dialog = Dialog(context)

        dialog!!.window!!.requestFeature(Window.FEATURE_NO_TITLE)
        dialog!!.setContentView(R.layout.dialog_internet_check)
        dialog!!.setCanceledOnTouchOutside(false)

        val btnOk = dialog!!.btn_ok

        btnOk.setOnClickListener {
            dialog!!.dismiss()

            (context as Activity).finishAndRemoveTask()
            System.runFinalizersOnExit(true)
            exitProcess(0)
        }

        val decorView = dialog!!.window?.decorView
        decorView?.systemUiVisibility = (
                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                        or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                        or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY // hide status bar and nav bar after a short delay, or if the user interacts with the middle of the screen
                )

        dialog!!.show()
    }

    private fun isAirplaneModeOn(context: Context): Boolean {
        return Settings.System.getInt(context.contentResolver,
                Settings.Global.AIRPLANE_MODE_ON, 0) !== 0
    }
}

这是NetWorkUtil

class NetworkUtil {
    companion object {
        private const val TYPE_WIFI = 1
        private const val TYPE_MOBILE = 2
        private const val TYPE_NOT_CONNECTED = 0

        private fun getConnectivityStatus(context: Context): Int {
            val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val networkInfo = cm.activeNetworkInfo
            if (null != networkInfo) {
                when (networkInfo.type) {
                    ConnectivityManager.TYPE_WIFI -> {
                        return TYPE_WIFI
                    }
                    ConnectivityManager.TYPE_MOBILE -> {
                        return TYPE_MOBILE
                    }
                    else -> {
                    }
                }
            }
            return TYPE_NOT_CONNECTED
        }

        fun getConnectivityStatusString(context: Context): Boolean {
            val conn = getConnectivityStatus(context)
            var status = false
            if (conn == TYPE_WIFI) {
                status = true
            } else if (conn == TYPE_MOBILE) {
                status = true
            } else if (conn == TYPE_NOT_CONNECTED) {
                status = false
            }
            return status
        }
    }
}

然后我在manifests.xml

中添加了它
        <receiver android:name="receiver.NetworkChangeReceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>

在清单中定义CONNECTIVITY_CHANGE已被弃用,所以我以编程方式实现了。

MainActivity

        // Internet Check Receiver
        val intentFilter = IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")
        this.registerReceiver(NetworkChangeReceiver(), intentFilter)

然后我打开ProGuard并启用了Multidex。

你有什么想法吗?

0 个答案:

没有答案