define the variables in your activity
like this:
const val SYSTEM_DIALOG_REASON_KEY = "reason"
const val SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"
const val SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"
define your broadcast receiver class like this:
class ServiceActionsReceiver: BroadcastReceiver(){
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent!!.action
if (action == Intent.ACTION_CLOSE_SYSTEM_DIALOGS) {
val reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY)
if (reason != null) {
if (reason == SYSTEM_DIALOG_REASON_HOME_KEY) {
//do what you want to do when home pressed
} else if (reason == SYSTEM_DIALOG_REASON_RECENT_APPS) {
//do what you want to do when recent apps pressed
}
}
}
}
}
register reciver on onCreate
method or onResume
method like this:
val filter = IntentFilter()
filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
registerReceiver(receiver, filter)
add receiver in your manifest like this:
<receiver android:name=".ServiceActionsReceiver">
<intent-filter >
<actionandroid:name="android.intent.action.CLOSE_SYSTEM_DIALOGS"/>
</intent-filter>
</receiver>