Search code examples
javaandroidhomescreenlockscreen

Disable Home Button in Homescreen app?


I am working on a research project for my university. The app will never be put on the market and only used for research.

I have made a homescreen app by using the Google Homescreen sample code. In there, I have made an activity that is a lockscreen. While in there, the user should not be able to get out of the lock by pressing Home, Back, etc. The Back-Button seems to be disabled, but the Home-Button is not. I have tried several solutions from the internet and stackoverflow, which are all not working.

Here is the important code:

(Notice: Logcat shows "Button pressed: 4" for the Back-Button but nothing for the home button!)

In my Lock-Screen Activity:

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.v(TAG, "BUTTON PRESSED: " + new Integer(keyCode).toString());

        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            return true;
        } else if ((keyCode == KeyEvent.KEYCODE_CALL)) {
            return true;
        }
        else if ((keyCode == KeyEvent.KEYCODE_HOME)){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

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

It seems like the onAttachedToWindow() Method is not working since Android Version 4. How can I disable the homebutton?

EDIT: Manifest file:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.home" >

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

    <permission android:name="android.permission.WRITE_SECURE_SETTINGS" >
    </permission>

    <application
        android:icon="@drawable/ic_launcher_home"
        android:label="@string/home_title" >
        <service android:name=".MyService" >
        </service>

        <activity
            android:name="Home"
            android:launchMode="singleInstance"
            android:stateNotNeeded="true"
            android:theme="@style/Theme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


            </intent-filter>

            <receiver android:name=".ScreenReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.SCREEN_ON" />
                    <action android:name="android.intent.action.SCREEN_OFF" />
                    <action android:name="android.intent.action.USER_PRESENT" />
                </intent-filter>
            </receiver>
        </activity>
        <activity
            android:name="Wallpaper"
            android:icon="@drawable/bg_android_icon"
            android:label="Wallpaper" >
            <intent-filter>
                <action android:name="android.intent.action.SET_WALLPAPER" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".LockPage" >
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_ON" />
                <action android:name="android.intent.action.SCREEN_OFF" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <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" />
                <category android:name="android.intent.category.DEFAULT" />

                <action android:name="android.intent.action.MAIN" />

                <data android:mimeType="text/plain" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />

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

                <data android:scheme="http" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Solution

  • This may come a bit late, I have been on a somewhat similar situation. My problem was that I didn't want users to leave the call screen during phone calls but I couldn't prevent it, so instead I simply brought it back front every time they left it.

    In your case you could simply bring your app back to front on pause:

    @Override
    protected void onPause() {
        super.onPause();
            // Close and reopen app or bringToFront()
    }
    

    So leaving would automatically open the app again. You should try either reopening your activity or bringing it to front and see what works best. Reopening may be unnoticeable if you remove all animations and add FLAG_ACTIVITY_NO_ANIMATION.