【发布时间】:2011-07-29 16:51:20
【问题描述】:
我想在我的 android 上创建自己的“主”屏幕,并且我想从我的应用程序中调用该主屏幕。
如何覆盖“主页”按钮,以便在按下它时应用程序将重定向到我的主屏幕而不是默认主屏幕?是否可以覆盖主页按钮?
【问题讨论】:
-
查看this post,其中解决了制作能够捕捉主页按钮的主页启动器。
标签: android
我想在我的 android 上创建自己的“主”屏幕,并且我想从我的应用程序中调用该主屏幕。
如何覆盖“主页”按钮,以便在按下它时应用程序将重定向到我的主屏幕而不是默认主屏幕?是否可以覆盖主页按钮?
【问题讨论】:
标签: android
主页按钮应该持续地只做一件事和一件事。让用户回到主屏幕。即使您可以覆盖它的行为,这也是一件对用户非常不友好的事情。所以不要这样做并以不同的方式解决您的问题!
【讨论】:
这个答案将不再有效,从 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);
}
【讨论】:
Activity.onKeyDown() 后立即返回 true 进行了测试:所有按钮确实都被禁用了,除了主页按钮! 但后来我尝试了下面@ucasneo 的onAttachedToWindow() 技巧,主页按钮也被禁用!幸运的是,长按返回按钮保存了我不必重新启动手机...
onAttachedToWindow() 在每次活动启动时只被调用一次,所以如果你禁用了home 按钮(必须是在活动刚开始时),你不能重新启用它该活动的生命周期。因此,即使这种方法有效,它也有很大的局限性。
覆盖下面的方法。
@Override
public void onAttachedToWindow()
{
Log.i("TESTE", "onAttachedToWindow");
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
使用此方法,主页按钮停止在此活动中工作(仅此活动)。然后你只需重新实现它,因为它是一个普通的按钮事件(例如后退按钮)。
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
<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");
}
}
你没有得到关键事件
【讨论】:
在活动中试试这个
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
【讨论】:
不,我们不能覆盖主页按钮,但我为此提供了一个解决方案....:
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" />
【讨论】:
如果有人需要检测和控制 HOME 按钮行为,请在 KOTLIN 中使用 thois 方法
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
}
【讨论】: