SocketService.kt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.jld.vod.service
  2. import android.app.Service
  3. import android.content.Intent
  4. import android.os.Handler
  5. import android.os.IBinder
  6. import android.os.Message
  7. import android.view.WindowManager
  8. import com.google.gson.Gson
  9. import com.jld.vod.R
  10. import com.jld.vod.config.Config
  11. import com.jld.vod.model.bean.GEOAdvertBean
  12. import com.jld.vod.model.bean.ResultBean
  13. import com.jld.vod.model.event.ReceiveEvent
  14. import com.jld.vod.socket.ReceiveSocketManager
  15. import com.jld.vod.socket.SendSocketManager
  16. import com.jld.vod.utils.LogUtils
  17. import com.jld.vod.utils.SharedPrefUtils
  18. import com.jld.vod.utils.http.OkHttpUtils
  19. import com.jld.vod.view.SplashActivity
  20. import com.jld.vod.view.widget.CustomAdDialog
  21. import com.jld.vod.view.widget.CustomBlackBgDialog
  22. import com.jld.vod.view.widget.CustomGEOAdDialog
  23. import com.jld.vod.view.widget.CustomScreenAdDialog
  24. import okhttp3.Call
  25. import okhttp3.Response
  26. import org.greenrobot.eventbus.EventBus
  27. import org.greenrobot.eventbus.Subscribe
  28. import org.greenrobot.eventbus.ThreadMode
  29. import org.json.JSONObject
  30. import java.io.IOException
  31. /**
  32. * upd服务
  33. */
  34. class SocketService : Service() {
  35. private var sendSocketManager: SendSocketManager? = null
  36. private var receiveSocketManager: ReceiveSocketManager? = null
  37. private var mBgCustom : CustomBlackBgDialog? = null
  38. private var mGEOCustom : CustomScreenAdDialog? = null
  39. private var gson:Gson?= null
  40. private val BLACKBG : Int = 0x10
  41. private val GEOAD :Int = 0x11
  42. private val DISGEOAD :Int = 0x12
  43. private val mHandler = Handler(Handler.Callback { message ->
  44. when(message.what)
  45. {
  46. BLACKBG->{
  47. val msg = message.obj.toString()
  48. if (msg == "true")
  49. {
  50. if (mBgCustom == null)
  51. {
  52. mBgCustom = CustomBlackBgDialog(this)
  53. mBgCustom!!.getWindow()!!.setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
  54. mBgCustom!!.show()
  55. }
  56. }else if(msg == "false")
  57. {
  58. if (mBgCustom != null)
  59. {
  60. mBgCustom!!.dismiss()
  61. mBgCustom = null
  62. }
  63. }
  64. }
  65. GEOAD->{
  66. val msgs = message.obj as GEOAdvertBean
  67. if (msgs.data != null)
  68. {
  69. if (mGEOCustom == null)
  70. {
  71. mGEOCustom = CustomScreenAdDialog(this)
  72. mGEOCustom!!.getWindow()!!.setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
  73. }
  74. mGEOCustom!!.setDataList(msgs.data)
  75. mGEOCustom!!.show()
  76. }
  77. }
  78. DISGEOAD->{
  79. if (mGEOCustom != null)
  80. {
  81. mGEOCustom!!.dismiss()
  82. mGEOCustom = null
  83. }
  84. }
  85. }
  86. false
  87. })
  88. override fun onCreate() {
  89. EventBus.getDefault().register(this)
  90. receiveSocketManager = ReceiveSocketManager.getInstance(this)
  91. receiveSocketManager!!.startUdpConnection()
  92. //初始化gson
  93. gson = Gson()
  94. super.onCreate()
  95. }
  96. /**
  97. * 接收UDP广播
  98. */
  99. @Subscribe(threadMode = ThreadMode.MAIN)
  100. fun onReceive( action : ReceiveEvent?) {
  101. LogUtils.logD("接收广播"+action)
  102. if (action!!.data == null)return
  103. val jsonObject = JSONObject(action!!.data)
  104. try {
  105. val type = jsonObject.optString("type")
  106. val msg = jsonObject.optString("msg")
  107. when (type)
  108. {
  109. "CLOSE"->{
  110. receiveSocketManager!!.stopSocket()
  111. }
  112. Config.SCREEN->{
  113. val msgs = Message()
  114. msgs.what = BLACKBG
  115. msgs.obj = msg
  116. mHandler.sendMessage(msgs)
  117. }
  118. Config.ADS->{
  119. if (msg == "true")
  120. {
  121. initAdVert()
  122. }else if(msg == "false")
  123. {
  124. val msgs = Message()
  125. msgs.what = DISGEOAD
  126. mHandler.sendMessage(msgs)
  127. }
  128. }
  129. }
  130. }catch (e:Exception)
  131. {
  132. e.printStackTrace()
  133. }
  134. }
  135. /**
  136. * 初始化广告模块
  137. */
  138. private fun initAdVert() {
  139. OkHttpUtils.getInstance().getDataAsyn(Config.ApiBaseUrl+"/advert/findPositionAdvert",object : OkHttpUtils.MyNetCall{
  140. override fun failed(call: Call?, e: IOException?) {
  141. LogUtils.logE(e.toString())
  142. }
  143. override fun success(call: Call?, response: Response?) {
  144. val json = response!!.body!!.string()
  145. val mGEOAdvertBean= gson!!.fromJson(json, GEOAdvertBean::class.java)
  146. val msg = Message()
  147. msg.what = GEOAD
  148. msg.obj = mGEOAdvertBean
  149. mHandler.sendMessage(msg)
  150. }
  151. })
  152. }
  153. override fun onBind(intent: Intent?): IBinder? {
  154. return null
  155. }
  156. }