package zhangphil.home; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends Activity { private MyReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); receiver = new MyReceiver(); IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); registerReceiver(receiver, homeFilter); } @Override public void onDestroy() { unregisterReceiver(receiver); super.onDestroy(); } private class MyReceiver extends BroadcastReceiver { private final String SYSTEM_DIALOG_REASON_KEY = "reason"; private final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; private final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY); if (reason == null) return; // Home键 if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) { Toast.makeText(getApplicationContext(), "按了Home键", Toast.LENGTH_SHORT).show(); } // 最近任务列表键 if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) { Toast.makeText(getApplicationContext(), "按了最近任务列表", Toast.LENGTH_SHORT).show(); } } } } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/3159.html