How to disable the home key

I want to lock the screen. I want to disable the home key and use only the return key. How to do it?

+30
android
Oct 10 '10 at 3:20
source share
5 answers

Use this method to disable the Home key in android

@Override public void onAttachedToWindow() { this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); } 
+29
Nov 01 2018-11-11T00:
source share

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

Your application is now an alternative Launcher application.

Use adb and disable launcher application using package manager

 pm disable com.android.launcher2 

Now pressing the Home key will always remain on one screen.

+22
Dec 27 '10 at 11:45
source share

This solution only works with 3.x.

Well, that should have been a difficult question. But here is a way to crack it.

Cancel the method below in your activity,

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

And now handle a 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; } 
+12
Jan 17 '12 at 5:05
source share

Add this code to the 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(); } } 

This is not a lock for the home button, but the user cannot switch to another application for a long time (more than 100 milliseconds), maybe this is what you want.

+4
Nov 19 '15 at 13:54 on
source share

To disable the home button, try the following:

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

The problem with the notification bar turned off can be solved by hiding the notification bar, as 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 a full-screen theme for your activity or application in the manifest:

 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
0
Mar 11 '13 at 0:27
source share



All Articles