繁体   English   中英

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

[英]My Service won't stop even if is onDestroy fired

我有一个行为类似于TimerTaskService ,即使我使用以下命令停止了该服务:

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

它没有停止,我在onDestroy有一个Log并且该Log已被触发...但是我认为问题是我在Service内使用TimerTask ,这是我的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)

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

编辑

我的方法是从现在开始,但是我不知道这是否是最好的方法:

override fun onDestroy() {
    super.onDestroy()
    Log.e("Service finish", "Finish")
    if(mTimer!=null){
        mTimer!!.cancel()
        mTimer!!.purge()
    }
}

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM