我可以覆盖我的应用程序中的“主页”按钮吗?

我想在我的android上创建我自己的“家”屏幕,我想从我的应用程序调用主屏幕。

我怎样才能覆盖“主页”按钮,以便当它被按下时,应用程序将被重定向到我的主屏幕,而不是默认的主屏幕? 是否有可能重写主页按钮?

嗨再次您可以覆盖主页按钮作为任何其他按钮:

@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_HOME)) { Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show(); return true; } return super.onKeyDown(keyCode, event); } 

覆盖下面的方法。

 @Override public void onAttachedToWindow() { Log.i("TESTE", "onAttachedToWindow"); this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); } 

使用此方法,HOME按钮停止在此活动中工作(仅此活动)。 然后你只需重新实现,因为这是一个正常的按钮事件(例如后退按钮)。

 public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_HOME) { Log.i("TESTE", "BOTAO HOME"); return true; } return super.onKeyDown(keyCode, event); } 

AndroidManifest.xml

       ....   

你需要launchMode="singleTask"所以意图被传递到已经运行的应用程序,而不是创建一个新的实例。

在活动中:

  @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (Intent.ACTION_MAIN.equals(intent.getAction())) { Log.i("MyLauncher", "onNewIntent: HOME Key"); } } 

你没有得到一个关键的事件

主页按钮应该只做一件事,一件事而且始终如一。 让用户回到主屏幕。 即使你可以覆盖它的行为,这将是一个非常用户不友好的事情。 所以不要这样做,以不同的方式解决你的问题!

试试这个活动

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); break; default: return super.onOptionsItemSelected(item); } return false; } 

如果有人需要检测和ovveride HOME按钮的行为使用KOTLIN thois appproach

 import android.content.Intent import android.content.BroadcastReceiver import android.content.Context import android.content.IntentFilter import android.util.Log class HomeWatcher(context: Context) { val TAG = "hg" private var mContext: Context? = null private var mFilter: IntentFilter? = null private var mListener: OnHomePressedListener? = null private var mRecevier: InnerRecevier? = null // initializer block init { mContext = context mFilter = IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS) } fun setOnHomePressedListener(listener: OnHomePressedListener) { mListener = listener mRecevier = InnerRecevier() } fun startWatch() { if (mRecevier != null) { this.mContext!!.registerReceiver(mRecevier, mFilter) } } fun stopWatch() { if (mRecevier != null) { this.mContext!!.unregisterReceiver(mRecevier) } } internal inner class InnerRecevier : BroadcastReceiver() { val SYSTEM_DIALOG_REASON_KEY = "reason" val SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions" val SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps" val SYSTEM_DIALOG_REASON_HOME_KEY = "homekey" override fun onReceive(context: Context, intent: Intent) { val action = intent.action if (action == Intent.ACTION_CLOSE_SYSTEM_DIALOGS) { val reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY) if (reason != null) { Log.e(TAG, "action:$action,reason:$reason") if (mListener != null) { if (reason == SYSTEM_DIALOG_REASON_HOME_KEY) { mListener!!.onHomePressed() } else if (reason == SYSTEM_DIALOG_REASON_RECENT_APPS) { mListener!!.onHomeLongPressed() } } } } } } } 

主要活动

 class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { private var launchers = ArrayList() private var mStoredPrimaryColor = 0 private var mStoredTextColor = 0 private var mStoredUseEnglish = false private var receiver: BroadcastReceiver? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) HomeButtonWatcher() } fun HomeButtonWatcher() { val mHomeWatcher = HomeWatcher(applicationContext) mHomeWatcher.setOnHomePressedListener(object : OnHomePressedListener { override fun onHomePressed() { // do something here... Toast.makeText(applicationContext, "onHomePressed", Toast.LENGTH_LONG).show() } override fun onHomeLongPressed() { // do something here... Toast.makeText(applicationContext, "onHomeLongPressed", Toast.LENGTH_LONG).show() } }) mHomeWatcher.startWatch() } // Other code } 

不,我们不能重写主页按钮,但我资助这个….的解决方案:

 public boolean isApplicationSentToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } @Override public void onStop() { if (isApplicationSentToBackground(this)){ //put your code here what u want to do } super.onStop(); } 

更改为清单文件 –