我想在我的Android上创建自己的“主页”屏幕,我想从我的应用程序中调用该主屏幕。
如何覆盖“主页”按钮,以便在按下时应用程序将重定向到我的主屏幕而不是默认的主屏幕?是否可以覆盖主页按钮?
答案 0 :(得分:12)
覆盖以下方法。
@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);
}
答案 1 :(得分:10)
在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"
,以便将意图传递给已在运行的应用,而不是创建新实例。
在活动中:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Log.i("MyLauncher", "onNewIntent: HOME Key");
}
}
你没有得到关键事件
答案 2 :(得分:5)
主页按钮应该只做一件事而且一件事。让用户返回HOME屏幕。即使你可以覆盖它的行为,这将是一个非常用户不友好的事情。所以不要这样做,以不同的方式解决你的问题!
答案 3 :(得分:2)
这个答案将不再适用,而不是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);
}
答案 4 :(得分:0)
在活动中尝试此操作
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
答案 5 :(得分:0)
不,我们无法覆盖主页按钮,但我为此提供了解决方案......:
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" />
答案 6 :(得分:0)
如果有人需要在KOTLIN中使用thois appproach来检测并且使用HOME按钮行为
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
}