java - 不重新启动就不能强制关闭应用程序吗?

标签 java android kotlin

我正在使用此代码在 1 秒后强制关闭我的应用程序。但问题是它关闭后会自己重启!我怎样才能让它永久关闭?

  private fun endApp() {

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

最佳答案

使用此方法强制退出您的应用:

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.

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()
}

关于java - 不重新启动就不能强制关闭应用程序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63385441/

相关文章:

android - Room 不能选择一个构造函数,因为多个构造函数是合适的错误

kotlin - collectAsState 未从存储库接收更改

java - Spring Security 3.1 重定向到登录不起作用

android - 迁移到 androidx 后 BottomNavigationView 膨胀错误

android - 弹出式菜单离开屏幕

Android 虚拟设备 (AVD) -- 我应该选择哪个 CPU/ABI?

android - 关于暂停和延期的概念问题

java.security.AccessControlException : access denied ("java.net.SocketPermission" "localhost:10648" "listen,resolve")

Java - 正负零的比较

java - 将 8 位索引颜色转换为 RGB