Compared to the homepage key (home) and the most recent application key (App_switch) processing, the return key is relatively simple, the replication onkeydown can be implemented, as follows:
@Override Public boolean onKeyDown (int keycode, keyevent event) { // keyevent.keycode_home Keyevent.keycode_app_switch if (keycode = = keyevent.keycode_back) { exitaction (); return true; } else return Super.onkeydown (KeyCode, event); }
Home and App_switch are more complex, especially home keys, which can be exploited by malicious applications if not specifically set. So even if the home button is processed, the measurement will first enter the launcher, a period of time before the execution of our operation, presumably this is also the system layer from a security perspective, there is time to look at the system source code.
After trying many ways, the Onuserleavehint function is more convenient, as follows:
@Override protected void Onuserleavehint () { //super.onuserleavehint (); Exitaction (); }
The operation is written in the Exitaction function, which takes the startup setting as an example:
private void Exitaction () { try { Intent Intent = new Intent (); ComponentName componentname = new ComponentName ("Com.android.settings", "com.android.settings.Settings"); Intent.setcomponent (componentname); StartActivity (intent); } catch (Exception e) { e.printstacktrace (); } }
Reprint Please specify source: Zhou Mushi's CSDN blogHttp://blog.csdn.net/zhoumushui
my github: Zhou Mushi's GitHubHttps://github.com/zhoumushui
Android Home key and recent app key processing Onuserleavehint