Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 721
hamena314 Avatar asked Jul 03 '12 09:07

hamena314


4 Answers

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.

like image 185
lisovaccaro Avatar answered Nov 06 '22 18:11

lisovaccaro


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!

like image 28
hamena314 Avatar answered Nov 06 '22 19:11

hamena314


@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;
}
like image 2
Kuru Kshetran Avatar answered Nov 06 '22 19:11

Kuru Kshetran


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

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

like image 1
Martze Avatar answered Nov 06 '22 18:11

Martze