How to disable the home key

2019-01-01 08:43发布

I'd like to lock the screen. I want to disable the home key and only use the back key. How do I accomplish this?

5条回答
刘海飞了
2楼-- · 2019-01-01 09:01

Use this method to disable the Home key in android

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}
查看更多
冷夜・残月
3楼-- · 2019-01-01 09:07

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.

查看更多
路过你的时光
4楼-- · 2019-01-01 09:13

This solution works up to 3.x only.

Okay, this was supposed to be a hard question. But here is a way to crack it.

Override the below method in your Activity,

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

And now handle the key event like this,

@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;
}
查看更多
公子世无双
5楼-- · 2019-01-01 09:13

To disable home button, try this:

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

Problem with notification bar pulled down can be solved by hiding notification bar like here:

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"
查看更多
永恒的永恒
6楼-- · 2019-01-01 09:25

I found a way to tackle the HOME key. For your application set the manifest as

<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"/>

Now your application is an alternate Launcher application.

Use the adb, and disable the launcher application using package manager

pm disable com.android.launcher2

Now the Home key press will always stay in the same screen.

查看更多
登录 后发表回答