liugl
2021-01-14 40f9ac00ec5e4d6363450d1c8e0b0937cc0b1de4
app/src/main/java/com/okgoincar/base/MyApplication.kt
@@ -3,12 +3,12 @@
import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.multidex.MultiDex
import cn.sinata.rxnetty.Cache.CacheUtil
import cn.sinata.rxnetty.NettyClient
import cn.sinata.xldutils.BaseApplication
import cn.sinata.xldutils.utils.sysErr
@@ -29,6 +29,7 @@
import com.okgoincar.netUtls.getUserId
import com.okgoincar.slab.MySlabBaseActivity
import com.okgoincar.utils.Cache.CacheKey
import com.tencent.bugly.crashreport.CrashReport
import org.jetbrains.anko.toast
import org.json.JSONObject
import java.security.SecureRandom
@@ -69,12 +70,15 @@
//        PlatformConfig.setWeixin("wx65d2c03f04352f90", "7a3a57f9ba78c1f400b75298eec4c7c0")
//        PlatformConfig.setQQZone("101877032", "eab94b262bf9789eec770cde42a6e71e") //qq互联
//        LogUtils.OpenLog(true)
        CrashReport.initCrashReport(applicationContext, "666d35fada", false)
        initUpPoint()
        initSocket()
    }
    private fun initSocket() {
        Handler(Looper.getMainLooper()).post {
//            toast("websocket初始化")
        }
        NettyClient.getInstance().init(this, Api.SOCKET_SERVER, Api.SOCKET_PORT, true)
        NettyClient.getInstance()
            .addOnMessageListener { message: String? ->
@@ -84,9 +88,9 @@
                    currentTime = System.currentTimeMillis()
                    val json = JSONObject(message)
                    val method = json.optString("method")
//                    Handler(Looper.getMainLooper()).post {
                    Handler(Looper.getMainLooper()).post {
//                        toast("收到websocket=$method")
//                    }
                    }
                    val data = json.optString("data")
                    val code = json.optInt("code", -1)
                    if (code == 200) {
@@ -158,6 +162,7 @@
            .setOnConnectListener {
                sendHeart()
            }
        sendHeart()
    }
    override fun attachBaseContext(base: Context?) {
@@ -259,13 +264,13 @@
            NettyClient.getInstance()
                .sendMessage(Gson().toJson(bean))
            Handler(Looper.getMainLooper()).post {
                toast("发送location --> $dataBean")
//                toast("发送location --> $dataBean")
            }
            sysErr(dataBean.toString())
        }
    }
    private fun sendHeart() {
    open fun sendHeart() {
        if (getUserId() != -1) {
            Log.i(TAG, "sendHeart: userId === " + getUserId())
            val bean = HeartBean()
@@ -281,7 +286,13 @@
            if ((System.currentTimeMillis() - currentTime) / 1000 > 30) {
                sysErr("sendHeart_websocket 重新连接")
                currentTime = System.currentTimeMillis()
                NettyClient.getInstance().checkNettyState()
//                var s = CacheUtil.get().getAsString("reConnect")
//                s = "$s###初始化重连"
//                CacheUtil.get().put("reConnect", s)
//                NettyClient.getInstance().checkNettyState()
                NettyClient.getInstance().checkNettyStateNull() //reconnect
//                initSocket()
                heartHandler!!.postDelayed({ sendHeart() }, 5000)
            } else {
                sysErr("sendHeart_websocket 发送心跳")
@@ -321,8 +332,8 @@
                    return Gson().fromJson(locationStr, AMapLocation::class.java)
                } else {
                    val amap = AMapLocation("")
                    amap.latitude = -1.0
                    amap.longitude = -1.0
                    amap.latitude = 0.0
                    amap.longitude = 0.0
                    return amap
                }
            } else {
@@ -400,8 +411,7 @@
        showNum--
        Handler(Looper.getMainLooper()).postDelayed({
            if (showNum == 0) {
                setVideoText("OK出行司机已置于后台,可能会导致听单异常,里程计费差异,请勿切换。")
//                getTTsManager().setVideoText("OK出行司机已置于后台,可能会导致听单异常,里程计费差异,请勿切换。")
                getTTsManager().setVideoText("OK出行司机已置于后台,可能会导致听单异常,里程计费差异,请勿切换。")
            }
        }, 600)
@@ -411,11 +421,5 @@
        activities.add(activity)
    }
    fun setVideoText(s: String?) {
        val intent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.action = "com.wisdom.gps.action.tts"
        intent.putExtra("msg", s)
        sendBroadcast(intent)
    }
}