try { val intent = Intent( "android.intent.action.ACTION_REQUEST_SHUTDOWN") intent.putExtra("android.intent.extra.KEY_CONFIRM", true) // 其中false換成true,會彈出是否關機的確認窗口 intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK context!!.startActivity(intent) } catch (e: Throwable) { e.printStackTrace() }
或者直接發送廣播
// val intent:Intent = Intent() // intent.action = Intent.ACTION_SHUTDOWN // context!!.sendBroadcast(intent)
以上功能都需要系統權限