35

我想锁屏。我想禁用主页键,只使用返回键。我该如何做到这一点?

4

5 回答 5

30

使用此方法禁用 android 中的 Home 键

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}
于 2011-11-01T09:22:05.313 回答
23

我找到了解决 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 键按下将始终保持在同一屏幕上。

于 2010-12-27T11:45:57.767 回答
13

此解决方案仅适用于 3.x。

好吧,这应该是一个很难的问题。但这里有一种破解它的方法。

在您的活动中覆盖以下方法,

@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;
}
于 2012-01-17T05:05:09.683 回答
9

Add this code to your MainActivity class:

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

It's not a locking for 'home' button, but user can't switch to another app for a long time (more than 100 milliseconds), maybe this is what you want.

于 2015-11-19T13:54:28.257 回答
1

要禁用主页按钮,请尝试以下操作:

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

or set fullscreen theme for your Activity or Application in manifest:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
于 2013-03-11T00:27:41.937 回答