无法强制关闭应用而不重新启动吗?

huangapple go评论80阅读模式
英文:

Can't force close app without it restarting?

问题

I'm using this code to force close my app after 1 second. But the problem is that it restarts itself after closing! How can I make it close permanently?

我正在使用这段代码在1秒后强制关闭我的应用程序。但问题是它在关闭后重新启动了!如何使其永久关闭?

英文:

I'm using this code to force close my app after 1 second. But the problem is that it restarts itself after closing! How can I make it close permanently?

  private fun endApp() {

    val handler = Handler()
    handler.postDelayed({ Process.sendSignal(Process.myPid(), Process.SIGNAL_KILL) }, 1000)
  }

答案1

得分: 1

使用以下方法强制退出您的应用程序:

fun exitApp() {
    moveTaskToBack(true);
    exitProcess(0)
}

exitProcess(status: Int): 终止当前正在运行的进程。

moveTaskToBack (boolean nonRoot):将包含此活动的任务移动到活动堆栈的后面。任务内的活动顺序保持不变。

或者

fun exitApp() {
    finishAffinity()
}

finishAffinity():结束此活动以及当前任务中紧随其下具有相同关联性的所有活动。通常在应用程序可以启动到另一个任务(例如从其了解的内容类型的ACTION_VIEW)并且用户已使用向上导航从当前任务切换到自己的任务时使用。在这种情况下,如果用户已经进入了第二个应用程序的任何其他活动,所有这些活动都应作为任务切换的一部分从原始任务中移除。

英文:

Use this method to force exit your app:

fun exitApp() {
    moveTaskToBack(true);
    exitProcess(0)
}

> exitProcess(status: Int): Terminates the currently running
> process.
>
> moveTaskToBack (boolean nonRoot) Move the task containing this
> activity to the back of the activity stack. The activity's order
> within the task is unchanged.

or

> finishAffinity(): Finish this activity as well as all activities
> immediately below it in the current task that have the same affinity.
> This is typically used when an application can be launched on to
> another task (such as from an ACTION_VIEW of a content type it
> understands) and the user has used the up navigation to switch out of
> the current task and in to its own task. In this case, if the user has
> navigated down into any other activities of the second application,
> all of those should be removed from the original task as part of the
> task switch.

fun exitApp() {
    finishAffinity()
}

huangapple
  • 本文由 发表于 2020年8月13日 06:18:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63385441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定