First, the demand
In the application development process, start the service lock lock waiting for the service to return to unlock, in order to avoid clicking the home key to make the thread lock card dead bug, you need to monitor the home key, unlock the thread.
Second, the realization
Register and unregister are required for application.
public class Homewatcherreceiver extends Broadcastreceiver {
private static final String System_dialog_reason_key = "REASON";
private static final String System_dialog_reason_home_key = "HomeKey";
Private Onkeylistener listener;
@Override
public void OnReceive (context context, Intent Intent) {
String intentaction = Intent.getaction ();
if (Textutils.equals (Intentaction, intent.action_close_system_dialogs)) {
String reason = Intent.getstringextra (System_dialog_reason_key);
if (Textutils.equals (System_dialog_reason_home_key, REASON)) {
Listener.onhomekey ();
}
}
}
public void Registerhomekeyreceiver (context context) {
Intentfilter homefilter = new Intentfilter (intent.action_close_system_dialogs);
Context.registerreceiver (this, homefilter);
}
public void Unregisterhomekeyreceiver (context context) {
Context.unregisterreceiver (this);
}
public void Setlistener (Onkeylistener listener) {
This.listener = listener;
}
@FunctionalInterface
Public interface Onkeylistener {
void Onhomekey ();
}
}
Monitor the home button in Android