在APP的操作中,有时需要监听系统按键,今天我就分享下自己的学习所得。
Menu键和Back键通过KeyDown事件就可以监听到,但Home键触发是系统事件,不能通过KeyDown简单的监控,但可以通过监听广播事件来处理。按下Home键后,Activity也会调用onUserLeaveHint 和 onSaveInstanceState 等函数,也可以在里面添加想要的处理程序。
代码很简单,分享如下:
package com.example.hornsey.myapplication.Demo; import android.app.Activity; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import com.example.hornsey.myapplication.R; public class KeyDownDemo extends Activity { private static final String TAG = "KeyDownDemo"; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_key_down_demo); context = KeyDownDemo.this; /*Home键是系统事件,只能通过广播监听*/ IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); registerReceiver(receiver, filter); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: Log.d(TAG, "onKeyDown KEYCODE_BACK"); // showDialog(); break; /* * Home键是系统事件,不能通过KeyDown监听 * 此处log不会打印 */ case KeyEvent.KEYCODE_HOME: Log.d(TAG, "onKeyDown KEYCODE_HOME"); break; case KeyEvent.KEYCODE_MENU: Log.d(TAG, "onKeyDown KEYCODE_MENU"); break; } return super.onKeyDown(keyCode, event); } protected void showDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("系统提示"); builder.setMessage("确定要退出吗?"); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); } }); builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } @Override protected void onUserLeaveHint() { Log.d(TAG, "onUserLeaveHint "); super.onUserLeaveHint(); } @Override protected void onPause() { Log.d(TAG, "onPause "); super.onPause(); } @Override protected void onSaveInstanceState(Bundle outState) { Log.d(TAG, "onSaveInstanceState "); super.onSaveInstanceState(outState); } @Override protected void onDestroy() { Log.d(TAG, "onDestroy "); unregisterReceiver(receiver); super.onDestroy(); } /* *HomeKey监听事件广播接收器 * */ BroadcastReceiver receiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "HomeKeyEventBroadcastReceiver.onReceive "); } }; }
11-06 14:25:32.401 5442-5442/com.example.hornsey.myapplication D/KeyDownDemo﹕ onKeyDown KEYCODE_MENU 11-06 14:25:32.471 5442-5442/com.example.hornsey.myapplication D/AbsListView﹕ Get MotionRecognitionManager 11-06 14:25:37.471 5442-5442/com.example.hornsey.myapplication D/KeyDownDemo﹕ HomeKeyEventBroadcastReceiver.onReceive 11-06 14:25:37.511 5442-5442/com.example.hornsey.myapplication D/KeyDownDemo﹕ onUserLeaveHint 11-06 14:25:37.511 5442-5442/com.example.hornsey.myapplication D/KeyDownDemo﹕ onPause 11-06 14:25:38.221 5442-5442/com.example.hornsey.myapplication D/KeyDownDemo﹕ onSaveInstanceState 11-06 14:25:42.961 5442-5442/com.example.hornsey.myapplication D/KeyDownDemo﹕ HomeKeyEventBroadcastReceiver.onReceive 11-06 14:25:43.051 5442-5442/com.example.hornsey.myapplication D/KeyDownDemo﹕ HomeKeyEventBroadcastReceiver.onReceive 11-06 14:25:46.401 5442-5442/com.example.hornsey.myapplication E/copybit﹕ Error opening frame buffer errno=13 (Permission denied) 11-06 14:25:46.401 5442-5442/com.example.hornsey.myapplication W/Adreno200-EGLSUB﹕ <updater_create_surface_state:342>: updater_create_surface_state failed to open copybit, error: -13 11-06 14:25:52.421 5442-5442/com.example.hornsey.myapplication D/KeyDownDemo﹕ onKeyDown KEYCODE_BACK 11-06 14:25:52.611 5442-5442/com.example.hornsey.myapplication D/KeyDownDemo﹕ onPause 11-06 14:25:53.041 5442-5442/com.example.hornsey.myapplication D/KeyDownDemo﹕ onDestroy