【发布时间】:2011-04-23 08:51:15
【问题描述】:
我想锁定屏幕。我想禁用主页键,只使用返回键。我该如何做到这一点?
【问题讨论】:
-
嗨..你有解决办法吗。我也在尝试为我的锁屏应用禁用 Home 键,但它不起作用。
标签: android android-homebutton
我想锁定屏幕。我想禁用主页键,只使用返回键。我该如何做到这一点?
【问题讨论】:
标签: android android-homebutton
使用此方法禁用android中的Home键
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
【讨论】:
我找到了解决 HOME 键的方法。 对于您的应用程序,将清单设置为
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
现在您的应用程序是备用启动器应用程序。
使用 adb,并使用包管理器禁用启动器应用程序
pm disable com.android.launcher2
现在 Home 键按下将始终保持在同一屏幕上。
【讨论】:
此解决方案仅适用于 3.x。
好的,这应该是一个难题。但这里有一种破解方法。
在您的 Activity 中覆盖以下方法,
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
现在像这样处理关键事件,
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
}
【讨论】:
将此代码添加到您的 MainActivity 类中:
Timer timer;
MyTimerTask myTimerTask;
@Override
protected void onResume() {
super.onResume();
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Override
protected void onPause()
{
if (timer == null) {
myTimerTask = new MyTimerTask();
timer = new Timer();
timer.schedule(myTimerTask, 100, 100);
}
super.onPause();
}
private void bringApplicationToFront()
{
KeyguardManager myKeyManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
if( myKeyManager.inKeyguardRestrictedInputMode())
return;
Log.d("TAG", "====Bringging Application to Front====");
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
try
{
pendingIntent.send();
}
catch (PendingIntent.CanceledException e)
{
e.printStackTrace();
}
}
public void onBackPressed() {
// do not call super onBackPressed.
}
class MyTimerTask extends TimerTask {
@Override
public void run() {
bringApplicationToFront();
}
}
这不是锁定“主页”按钮,但用户无法长时间(超过 100 毫秒)切换到另一个应用程序,也许这就是你想要的。
【讨论】:
timer.schedule(myTimerTask, 100, 100);改为timer.schedule(myTimerTask, 1, 100);
要禁用主页按钮,请尝试以下操作:
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
通知栏下拉的问题可以通过隐藏通知栏来解决:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.xxxx);
getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);
....
}
或在清单中为您的活动或应用程序设置全屏主题:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
【讨论】: