像这样定义变量activity
:
const val SYSTEM_DIALOG_REASON_KEY = "reason"
const val SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"
const val SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"
像这样定义您的广播接收器类:
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
}
}
}
}
}
onCreate
在这样的方法或方法上注册接收者onResume
:
val filter = IntentFilter()
filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
registerReceiver(receiver, filter)
在清单中添加接收器,如下所示:
<receiver android:name=".ServiceActionsReceiver">
<intent-filter >
<actionandroid:name="android.intent.action.CLOSE_SYSTEM_DIALOGS"/>
</intent-filter>
</receiver>