我正在尝试为Android制作一个锁屏。每当我按下主页按钮时,它会将我带回主屏幕。我试过谷歌播放的各种应用程序。除非您解锁屏幕,否则 Go Locker 应用程序不允许您转到主屏幕。我不希望我的应用程序返回主屏幕,除非我刷屏幕解锁它。反正有没有做这个功能?已经3天了,我找不到任何相关内容。
非常感谢。
答案 0 :(得分:3)
以下是一些代码:
@Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
使用此方法,HOME按钮将停止在此活动中工作(仅限此活动)。然后你重新实现,因为这是一个普通的按钮事件。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
boolean res = super.onKeyDown(keyCode, event);
switch (keyCode) {
case KeyEvent.KEYCODE_HOME: {
// Your Home button logic;
return false;
}
return res;
}
警告: onAttachedToWindow()
覆盖这实际上有效,但如果禁用所有按钮,将用户锁定,可能会非常危险,只有退出正在删除的应用程序的方法电池,所以代码正确,处理所有情况。
OR
遇到这种覆盖主页按钮单击的方法,希望它适合您:
在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())) {
//your code goes here.
}
}
你没有得到关键事件。