Can I override the 'Home' button in my app

2019-01-02 15:32发布

I want to create my own 'home' screen on my android, and I want to call that home screen from my application.

How can I override the 'Home' button so that when it's pressed the application will be redirected to my home screen instead of the default home screen? Is it possible to override the home button?

标签: android
7条回答
其实,你不懂
2楼-- · 2019-01-02 16:13

The home button is supposed to do one thing and one thing only and consistently. Get the user back to the the HOME screen. Even if you could override it's behavior it would be an extremely user-unfriendly thing to do. So don't do it and solve your problem differently!

查看更多
泛滥B
3楼-- · 2019-01-02 16:15

This answer will no longer work, not since Android 4.0.

The correct solution is to create an app that can intercept the Home intent, as per @bara's answer below.


You can override the home button as any other button:

@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);
}
查看更多
ら面具成の殇う
4楼-- · 2019-01-02 16:16

No we cant override home button but i fund a solution for this....:

public boolean isApplicationSentToBackground(final Context context)  {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> 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();
}

make change to manifests file-

<uses-permission android:name="android.permission.GET_TASKS" />
查看更多
人间绝色
5楼-- · 2019-01-02 16:20

If someone need to detect and ovveride HOME button behavior use thois appproach in KOTLIN

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()
                        }
                    }
                }
            }
        }
    }
}

MainActivity

class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {

    private var launchers = ArrayList<AppLauncher>()
    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
}
查看更多
初与友歌
6楼-- · 2019-01-02 16:23

Try this in activity

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return false;
}
查看更多
明月照影归
7楼-- · 2019-01-02 16:30

In AndroidManifest.xml

<activity
    ...
    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />
        ....
    </intent-filter>
</activity>

You need launchMode="singleTask" so the intent is delivered to the already running app instead of creating a new instance.

In the activity:

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

        }
    }

You do not get a key event

查看更多
登录 后发表回答