Si alguien necesidad de detectar y ovveride INICIO comportamiento utilizar el botón Thois appproach en Kotlin
import android.content.Intent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.IntentFilter
import android.util.Log
class HomeWatcher(context: Context) {
val TAG = "hg"
private var mContext: Context? = null
private var mFilter: IntentFilter? = null
private var mListener: OnHomePressedListener? = null
private var mRecevier: InnerRecevier? = null
// initializer block
init {
mContext = context
mFilter = IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
}
fun setOnHomePressedListener(listener: OnHomePressedListener) {
mListener = listener
mRecevier = InnerRecevier()
}
fun startWatch() {
if (mRecevier != null) {
this.mContext!!.registerReceiver(mRecevier, mFilter)
}
}
fun stopWatch() {
if (mRecevier != null) {
this.mContext!!.unregisterReceiver(mRecevier)
}
}
internal inner class InnerRecevier : BroadcastReceiver() {
val SYSTEM_DIALOG_REASON_KEY = "reason"
val SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions"
val SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"
val SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"
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) {
Log.e(TAG, "action:$action,reason:$reason")
if (mListener != null) {
if (reason == SYSTEM_DIALOG_REASON_HOME_KEY) {
mListener!!.onHomePressed()
} else if (reason == SYSTEM_DIALOG_REASON_RECENT_APPS) {
mListener!!.onHomeLongPressed()
}
}
}
}
}
}
}
MainActivity
class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
private var launchers = ArrayList<AppLauncher>()
private var mStoredPrimaryColor = 0
private var mStoredTextColor = 0
private var mStoredUseEnglish = false
private var receiver: BroadcastReceiver? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
HomeButtonWatcher()
}
fun HomeButtonWatcher()
{
val mHomeWatcher = HomeWatcher(applicationContext)
mHomeWatcher.setOnHomePressedListener(object : OnHomePressedListener {
override fun onHomePressed() {
// do something here...
Toast.makeText(applicationContext, "onHomePressed", Toast.LENGTH_LONG).show()
}
override fun onHomeLongPressed() {
// do something here...
Toast.makeText(applicationContext, "onHomeLongPressed", Toast.LENGTH_LONG).show()
}
})
mHomeWatcher.startWatch()
}
// Other code
}
muchas gracias, ya que discutimos mucho sobre este tema ... esta Q. se le pregunta al mismo tiempo de ese HOME-Screen Launcher Q..y usted me dio soluciones ... gracias de nuevo ... – Smith
de nada. – Aleadam
Esto no funciona en ninguna versión de Android, AFAIK, y ciertamente no en nada reciente. – CommonsWare