0

I am new to android and developing android application with lock functionality.i want to disable device's home button in an activity and i am googled for it from last 4 days and i am not getting correct solution for it.i am tried all the solution that i get in below link,but i am not able to disable home button.please suggest me how to disable home and all device's key programmatically.any help would be appreciated.
here is the link that i visited and tried the solutions that described their.
How to disable android hardware buttons programmatically?

How to lock android buttons/phone from code (screen lock)?

How to disable the home key

http://sunil-android.blogspot.in/2013/07/mostly-used-android-code.html

http://www.technonutty.com/2013/11/disable-home-power-back-button-android.html

Here is the code that i lastly edited:
MainActivity.java

package com.example.lockdemo;   
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private LockLayer lockLayer;
    private View lockView;
    Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();

        button1 = (Button)lockView.findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                lockLayer.unlock();
                finish();
            }
        });
    }

    private void init() {
        lockView = View.inflate(this, R.layout.activity_main, null);

        lockLayer = LockLayer.getInstance(this);
        lockLayer.setLockView(lockView);
        lockLayer.lock();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

LockLayer.java

public class LockLayer {
    private Activity mActivty;
    private  WindowManager mWindowManager;
    private View mLockView;
    private LayoutParams mLockViewLayoutParams;
    private static LockLayer mLockLayer;
    private boolean isLocked;

    public static LockLayer getInstance(Activity act){
        if(mLockLayer == null){
            mLockLayer = new LockLayer(act);
        }
        return mLockLayer;
    }

    private LockLayer(Activity act) {
        mActivty = act;
        init();
    }

    private void init(){
        isLocked = false;
        mWindowManager = mActivty.getWindowManager();
        mLockViewLayoutParams = new LayoutParams();
        mLockViewLayoutParams.width = LayoutParams.MATCH_PARENT;
        mLockViewLayoutParams.height = LayoutParams.MATCH_PARENT;
        mLockViewLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
    }
    public void lock() {
        if(mLockView!=null){
            mWindowManager.addView(mLockView, mLockViewLayoutParams);
        }
        isLocked = true;
    }
    public void unlock() {
        if(mWindowManager!=null && isLocked){
            mWindowManager.removeView(mLockView);
        }
        isLocked = false;
    }
    public void setLockView(View v){
        mLockView = v;
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lockdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Thank you.

Community
  • 1
  • 1
Jat Dawson
  • 41
  • 1
  • 1
  • 2
  • 1
    Search about "System alert window"..it will help you to disable home button and recent app button – Darshil Shah Aug 10 '15 at 11:08
  • above coding is work on my android 4.4.2 device but only worked when activity is in recent app list.if i clear recent app list then it doesn't work. – Jat Dawson Aug 10 '15 at 11:16
  • You cant disable Home button. You should create a Launcher app – Emil Aug 10 '15 at 11:20
  • I am seen the lock app on play store that achieve that here is app link:https://play.google.com/store/apps/details?id=com.ln.lockscreen – Jat Dawson Aug 10 '15 at 11:24
  • Please refer this http://www.andreas-schrade.de/2015/02/16/android-tutorial-how-to-create-a-kiosk-mode-in-android/ – uday Aug 10 '15 at 11:31

3 Answers3

2

add this line in your project manifest.xml file

<uses-permission android:name="android.permission.REORDER_TASKS" /> 

and this one in your activity decrlaring tag

<category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />

in your activity modify onPause method like this

@Override
 protected void onPause() {
        super.onPause();

        ActivityManager activityManager = (ActivityManager) getApplicationContext()
                .getSystemService(Context.ACTIVITY_SERVICE);

        activityManager.moveTaskToFront(getTaskId(), 0);
}

overrride onKeyDown method in your activity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Do nothing or catch the keys you want to block
    //...
    return true; //Must return a boolean
}
anunezr
  • 59
  • 1
  • 7
Rob
  • 886
  • 9
  • 16
0

Firstly I want to say there is a reason the Home button is harder to disable than other keys - it shouldn't be done. You can imagine how many malicious apps would do it. Even if you do implement it, it will require some powerful permissions which will make people reconsider using your app. The only way I have seen this successfully done is with custom launchers

If you have a legitimate need, the following method should work - but the Notification bar will still be available, which is a separate issue from your question, but probably still relevant.

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

The same answer can be found here on a question which also contains some other useful answers

Community
  • 1
  • 1
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • Hmm unfortunately the home button isnt meant to be disabled. You can imagine how many malicious apps would do it. Even if you do implement it, it will require some powerful permissions which will make people reconsider using your app. The only way I have seen this successfully done is with custom launchers – Nick Cardoso Aug 10 '15 at 11:45
0

Disabling the Home is like the user can click on it but no action, in that case, you can make a full screen or you can hide the full navigation bar by using the below code in your activity class

    public void hideNavBtn(boolean hide){
//        if (hide || !hide) return; //for testing other way to lock
        WindowInsetsControllerCompat windowInsetsController =
                ViewCompat.getWindowInsetsController(getWindow().getDecorView());
        if (windowInsetsController == null) {
            return;
        }
        windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE);

        if(hide) {
            // Hide the system bars and nav bars.
//            windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
            windowInsetsController.hide(WindowInsetsCompat.Type.navigationBars());
        }
        else
        {
            // Show the system bars and nav bars.
//            windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
            windowInsetsController.show(WindowInsetsCompat.Type.navigationBars());
        }
    }

 @Override
    public boolean onTouchEvent(MotionEvent event) {
        hideNavBtn(true);
        return super.onTouchEvent(event);
    }

 @Override
    protected void onResume() {
        super.onResume();
        hideNavBtn(true);
    }

only by swipe from system bars it will appear or if you want to put the code

 hideNavBtn(false);

in any viewOnClickListner or optionMenu

Note: if you want to hide the system bars and nav bars both then just uncomment the below code and pass WindowInsetsCompat.Type.systemBars()

//int bothNavNSysBarHideNShow= WindowInsetsCompat.Type.systemBars()

     // windowInsetsController.hide(bothNavNSysBarHideNShow);
                    
    //windowInsetsController.show(bothNavNSysBarHideNShow);

instead of
WindowInsetsCompat.Type.navigationBars()

Naval Kishor Jha
  • 894
  • 9
  • 13