即使开启,我的服务也不会停止

时间:2019-04-16 16:53:48

标签: java android kotlin service

我有一个Service,其行为类似于TimerTask,即使我使用:停止了该服务,

val intent = Intent(applicationContext, TimeService::class.java)
stopService(intent)

它并没有停止,我在Log中有一个onDestroy并且这个Log被解雇了...但是我认为问题是我正在使用{{1 }} TimerTask内,这是我的Service

Service

问题是该日志:

class TimeService : Service() {
    private val mHandler = Handler()
    var calendar: Calendar? = null
    var simpleDateFormat: SimpleDateFormat? = null
    var strDate: String? = null
    var date_current: Date? = null
    var date_diff: Date? = null
    private var mTimer: Timer? = null
    private val NOTIFY_INTERVAL: Long = 1000
    var intent: Intent? = null

    companion object {
        val str_receiver = "myreceiver"
    }

    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()


        calendar = Calendar.getInstance()
        simpleDateFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())

        mTimer = Timer()
        mTimer!!.scheduleAtFixedRate(TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL)
        intent = Intent(str_receiver)
    }

    internal inner class TimeDisplayTimerTask : TimerTask() {

        override fun run() {
            mHandler.post {
                calendar = Calendar.getInstance()
                simpleDateFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
                strDate = simpleDateFormat!!.format(calendar!!.time)
                Log.e("strDate", strDate)
                twoDatesBetweenTime()
            }
        }

    }

    fun twoDatesBetweenTime(): String {


        try {
            date_current = simpleDateFormat!!.parse(strDate)
        } catch (e: Exception) {

        }

        try {
            date_diff = simpleDateFormat!!.parse(SharedPreferenceHelper.defaultPrefs(this).getString("data", ""))
        } catch (e: Exception) {

        }

        try {


            val diff = date_current!!.time - date_diff!!.time
            val timeInSeconds = Integer.valueOf(SharedPreferenceHelper.defaultPrefs(this).getString("seconds", "")!!)

            val timeTimer = TimeUnit.SECONDS.toMillis(timeInSeconds.toLong())
            val diffWithTime = timeTimer - diff
            val diffSeconds2 = diffWithTime / 1000 % 60
            val diffMinutes2 = diffWithTime / (60 * 1000) % 60
            val diffHours2 = diffWithTime / (60 * 60 * 1000) % 24


            if (diffWithTime >= 0) {
                val counterTime = "$diffHours2  :  $diffMinutes2 : $diffSeconds2"

                Log.e("TIME", counterTime)

                fn_update(counterTime)
            } else {
                SharedPreferenceHelper.defaultPrefs(this).edit().putBoolean("finish", true).apply()
                mTimer!!.cancel()
            }
        } catch (e: Exception) {
            mTimer!!.cancel()
            mTimer!!.purge()


        }

        return ""

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e("Service finish", "Finish")
    }

    private fun fn_update(str_time: String) {

        intent!!.putExtra("time", str_time)
        sendBroadcast(intent)
    }
}

此日志:

Log.e("strDate", strDate)

永不停止,我想念的是什么?

编辑

此刻我的方法是这样,但我不知道这是否是最好的方法:

Log.e("TIME", counterTime)

1 个答案:

答案 0 :(得分:2)

OnDestroy是系统调用的回调方法,以允许您的服务“干净退出”:

  

由系统调用以通知服务它已不再使用且已被删除。该服务此时应清除其拥有的所有资源(线程,注册的接收者等)。返回时,将不再有对该Service对象的调用,并且该对象实际上已失效。不要直接调用此方法。

在此回调之后,系统不会立即终止您的应用程序进程。此时您要杀死TimerTask。如果您让它继续运行,则认为是泄漏。很有可能它将一直运行,直到系统决定是时候终止您的应用程序进程为止,如果将其保留在前台,则可能需要一段时间。

相关问题