有使用到,所以就记下来,免得以后会忘记掉
1 public class HomeListener extends BroadcastReceiver { 2 private static final String LOG_TAG = "HomeReceiver"; 3 private static final String SYSTEM_DIALOG_REASON_KEY = "reason"; 4 private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"; 5 private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; 6 private static final String SYSTEM_DIALOG_REASON_LOCK = "lock"; 7 private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist"; 8 9 @Override 10 public void onReceive(Context context, Intent intent) { 11 String action = intent.getAction(); 12 Log.i(LOG_TAG, "onReceive: action: " + action); 13 if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { 14 // android.intent.action.CLOSE_SYSTEM_DIALOGS 15 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY); 16 Log.i(LOG_TAG, "reason: " + reason); 17 18 if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) { 19 // 短按Home键 20 Log.i(LOG_TAG, "homekey"); 21 22 } 23 else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) { 24 // 长按Home键 或者 activity切换键 25 Log.i(LOG_TAG, "long press home key or activity switch"); 26 27 } 28 else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) { 29 // 锁屏 30 Log.i(LOG_TAG, "lock"); 31 } 32 else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) { 33 // samsung 长按Home键 34 Log.i(LOG_TAG, "assist"); 35 } 36 37 } 38 } 39 }
然后程序中开始位置,注册广播
1 HomeListener mHomeKeyReceiver = new HomeListener(); 2 final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 3 this.registerReceiver(mHomeKeyReceiver, homeFilter);
时间: 2024-10-21 00:32:04