当前位置: 首页 > 知识库问答 >
问题:

如何在Android上停止IntentService?

巫马承德
2023-03-14

我的应用程序中正在运行IntentService。当用户按下“取消”按钮时,我想停止,但onHandleIntent会继续运行,即使在调用onDestroy(IntentService)时也是如此。

在执行过程中,我尝试了StSelfFe(),StutoIn(int)和StestService(意图),但不起作用。

class DownloadIntentService : IntentService("DownloadIntentService") {

    val TAG: String = "DownloadIntentService"

    val AVAILABLE_QUALITIES: Array<Int> = Array(5){240; 360; 480; 720; 1080}

    // TODO Configurations
    val PREFERED_LANGUAGE = "esLA"
    val PREFERED_QUALITY = AVAILABLE_QUALITIES[0]

    @Inject
    lateinit var getVilosDataInteractor: GetVilosInteractor

    @Inject
    lateinit var getM3U8Interactor: GetM3U8Interactor

    @Inject
    lateinit var downloadDataSource: DownloadsRoomDataSource

    companion object {
        var startedId: Int = 0
    }

    override fun onCreate() {
        super.onCreate()
        (application as CrunchApplication).component.inject(this)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.d(TAG, "onStartComand ${startId}")

        startedId = startId

        super.onStartCommand(intent, flags, startId)
        return Service.START_NOT_STICKY
    }

    override fun onHandleIntent(intent: Intent?) {
        Log.d(TAG, "starting download service")

        val download = downloadDataSource.getDownloadById(intent?.getLongExtra(MEDIA_ID_EXTRA, 0) ?: 0)

        Log.d(TAG, "A new download was found: ${download.id} ${download.serieName} ${download.collectionName} ${download.episodeName}")

        val vilosResponse = getVilosDataInteractor(download.episodeUrl)

        val stream: StreamData? = vilosResponse.streams.filter {
            it.hardsubLang?.equals(PREFERED_LANGUAGE) ?: false
        }.getOrNull(0)

        if(stream == null) {
            Log.d(TAG, "Stream not found with prefered language ($PREFERED_LANGUAGE)")
            return
        }

        Log.d(TAG, "Best stream option: " + stream.url)
        val m3u8Response = getM3U8Interactor(stream.url)

        val m3u8Data: M3U8Data? = m3u8Response.playlist.filter { it.height == PREFERED_QUALITY }[0]

        if(m3u8Data == null) {
            Log.d("M3U8","Resolution ${PREFERED_QUALITY}p not found")
            return
        }

        Log.d(TAG, m3u8Data.url)

        val root = Environment.getExternalStorageDirectory().toString()
        val myDir = File(root + "/episodes/");
        if (!myDir.exists()) {
            myDir.mkdirs()
        }

        val output = myDir.getAbsolutePath() + "EPISODENAME.mp4";
        val cmd = "-y -i ${m3u8Data.url} ${output}"

        when (val result: Int = FFmpeg.execute(cmd)) {
            Config.RETURN_CODE_SUCCESS -> Log.d(TAG, "Success")
            Config.RETURN_CODE_CANCEL -> Log.d(TAG, "Cancel")
            else -> Log.d(TAG, "Default: $result")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy")
    }
}

我试图从碎片中停下来

val intent = Intent(requireContext(), DownloadIntentService::class.java)
requireContext().stopService(intent)

提前感谢

共有1个答案

冉锋
2023-03-14

基本上你不能。OnHandleIntent在工作线程上运行。要停止它,你必须做与任何其他线程相同的事情,即在onHandleIntent中进行布尔标志检查,然后在执行操作之前检查该标志是否为真。现在,如果要取消,请将标志更新为false。

这也取决于你在做什么。如果已经在处理中,tat将继续运行。不是,你必须有状态机才能让它停止。

 类似资料:
  • 怎么停止这个计时器,知道吗? 我想重置计时器在每个查询,但它继续。每个新的查询都会添加新的计时器。这个怎么解决?

  • 当GCC的预处理器使用选项运行时,当它发现一个#错误时,有没有一种方法可以使它停止? 例如,在以下程序中:

  • 问题内容: 当电话状态恢复为IDLE时,我正在拨打电话并使用电话监听器重新开始活动。但是当我完成活动后,电话监听器仍在运行,因此,当用户进行自己的通话并挂断时,我的活动又重新启动! 问题答案: 使用作为参数传递给方法停止监听更新。

  • 我不知道怎么才能停止滑动菜单。我想删除可以滑动的幻灯片菜单。我试着用代码注释。但我无法停止扮演slidemenu。 下面是我的代码。你能帮助我吗?非常感谢!:)

  • 问题内容: 其实我的问题是,当我单击该项目时,它会转到DetailsFragment。但是,当我返回到ItemListFragment时,项目编号增加了,并且发生了重复。请帮助我从中恢复..预先感谢.... ItemListGridAdapter 详细片段 问题答案: 做这个 这样,您的数据将不再重复。试试看,让我知道它是否对您有用。

  • 问题内容: 我有这个jQuery ajax调用: 我想在条件返回的情况下停止调用,但返回不会停止调用。 我如何stop在Ajax上调用beforeSend? =======更新========= return false 也可以。 问题答案: $.ajax({ url : ‘my_action’, dataType: ‘script’, beforeSend : function(xhr, opt