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

cyvaqqii  于 2023-01-11  发布在  Android
关注(0)|答案(8)|浏览(154)

我想在我的android上创建我自己的“主”屏幕,我想从我的应用程序中调用那个主屏幕。
如何覆盖“主页”按钮,以便在按下该按钮时,应用程序将重定向到我的主屏幕,而不是默认的主屏幕?是否可以覆盖主页按钮?

myzjeezk

myzjeezk1#

重写下面的方法。

@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);    
}
8oomwypt

8oomwypt2#

输入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>

您需要launchMode="singleTask",以便将Intent交付给已在运行的应用,而不是创建新示例。
在活动中:

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

        }
    }

您不会获得关键事件

jw5wzhpr

jw5wzhpr3#

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

drkbr07n

drkbr07n4#

这个答案将不再工作,从Android 4.0.
正确的解决方案是创建an app that can intercept the Home intent, as per @bara's answer below
您可以像覆盖任何其他按钮一样覆盖主页按钮:

@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);
}
qojgxg4l

qojgxg4l5#

在活动中尝试此操作

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

nfs0ujit6#

如果有人需要检测和覆盖HOME按钮行为,请使用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()
                        }
                    }
                }
            }
        }
    }
}
    • 主要活动**
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
}
dly7yett

dly7yett7#

不,我们不能覆盖主页按钮,但我资助了一个解决方案。

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

更改清单文件-

<uses-permission android:name="android.permission.GET_TASKS" />
5sxhfpxr

5sxhfpxr8#

为什么要控制HOME按钮的场景。我想创建一个自定义锁屏界面,用户在给安卓设备充电时可以使用。这样他们就必须扫描特定代码才能再次登录。让应用禁用这些按钮(这样用户就无法离开应用)可以让应用保持用户看到的主屏幕。

相关问题