Disable Home Button in Homescreen app?
Asked Answered
D

4

9

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>
Disrespectful answered 3/7, 2012 at 9:16 Comment(6)
https://mcmap.net/q/83348/-how-to-disable-the-home-key/…Winchester
This seems to point to the exact same thing that I posted? Or am I missing the point you are trying to make?Disrespectful
Have you tried this one?Wizardry
Can you please post your manifest?Pelasgian
@LalitPoptani: IIRC this way of overriding the home button thankfully no longer works as of Android 4.0 – Kristopher Micinski Apr 26 at 12:57Disrespectful
@Martze: I have edited my first post, which now contains my ManifestDisrespectful
P
5

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.

Parcheesi answered 30/1, 2013 at 5:27 Comment(0)
D
2

It seems like the Home Button press is not forwarded to the app in a homescreen application. Therefore I made a normal app, put my broadcastReceiver and my service in and now I can disable the homebutton and the backbutton.

Still the recent Apps button can be used to jump out of my lockscreen. You can flood it with dummy entries which might work.

Hope that helps someone!

Disrespectful answered 6/7, 2012 at 11:14 Comment(0)
P
1

This is not possible without changing the Android source: [Mentioned here][1].

Also this would break the Android Activity Cycle, which is not recommended.

Pelasgian answered 3/7, 2012 at 9:26 Comment(1)
In the early version of the code, the program was only a single activity. I was able to point the home button to the activity itself and therefore using it only for this purpose. Maybe there is a way around?Disrespectful
L
1

this code works in my application

@Override 
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow(); 
}
Leathaleather answered 6/2, 2013 at 7:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.