Вопрос

My question is self explanatory. I have searched a lot but could not found the way to handle Recent activity button click. I want to ignore the hardware button clicks of Recent Activity button from the status bar from android 3.0 tablet.

Currently what I have tried so far is:

public boolean onKeyDown(int keyCode, KeyEvent event) {

       if(keyCode == KeyEvent.KEYCODE_BACK) 
       { 
              return true;
       }
       // in the same way I have written it for KEYCODE_HOME
}

can you tell me what should I write to handle recent activity button?

Thanking you in advance. :)

EDIT: This is what I have tried now. The KEYCODE_APP_SWITCH is not working.

public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("INSIDE", "LOCKDEMO");
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.e("KEY EVENT", "BACK");
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            Log.e("KEY EVENT", "HOME");
            return true;
        }
        if(keyCode == KeyEvent.FLAG_FROM_SYSTEM) {
            Log.e("KEY EVENT", "SYSTEM");
            return true;
        }
        if(keyCode == KeyEvent.FLAG_KEEP_TOUCH_MODE) {
            Log.e("KEY EVENT", "TOUCH MODE");
            return true;
        }
        if(keyCode == KeyEvent.FLAG_SOFT_KEYBOARD) {
            Log.e("KEY EVENT", "SoFT KEYBOARD");
            return true;
        }
        if(keyCode == KeyEvent.FLAG_VIRTUAL_HARD_KEY) {
            Log.e("KEY EVENT", "HARDWARE KEY");
            return true;
        }
        if(keyCode == KeyEvent.KEYCODE_APP_SWITCH) {
            Log.e("KEY EVENT", "APP SWITCH");
            return true;
        }

        Log.e("KEY EVENT", "NOT HANDLED");
        return super.onKeyDown(keyCode, event);
    }

When I press on RecentAppBtn, it does not even print the last Log statement i.e. Event not handled.

Это было полезно?

Решение

From peeking into the docs, I think that KeyEvent.KEYCODE_APP_SWITCH is what you're searching for.

You could however also use the KeyEvent.getKeyCode()-method (on the event-parameter) to see which key-code is triggered (and if there is any key triggered), when you press the application switcher.


I played around with this for a while and I have come to the conclusion, that it's not possible.

The KEYCODE_APP_SWITCH-event is not delivered to either onKeyDown() nor dispatchKeyEvent(). Ergo, It can not be handled.

Also, you'll run into problems on Android 4 devices, because the KEYCODE_HOME-event is also no longer dispatched to any of the above methods. See the docs:

Key code constant: Home key. This key is handled by the framework and is never delivered to applications.


It also seems that there is no real easy approach to creating a real lock-screen yourself. See this discussion: Developing a custom lock screen

It is well possible to show some custom information on the default lock-screen: Is there a way for me to show a customized message on the lock screen?


For the sake of completeness: In Android L, you'll be able to "lock users in your app", so you don't need to manually override the Hardware keys. Here's the relevant information:

The L Developer Preview introduces a new task locking API that lets you temporarily restrict users from leaving your app or being interrupted by notifications. This could be used, for example, if you are developing an education app to support high stakes assessment requirements on Android. Once your app activates this mode, users will not be able to see notifications, access other apps, or return to the Home screen, until your app exits the mode.

Другие советы

If you want to run your app rooted in Kiosk-mode, then it might be possible to disable com.android.systemui and replace buttons by soft buttons (for instance the Button Saviour app, though be careful not to brick your device!). It seems a lot of cosmetic stuff disappears and I don't know if it affects system stability in some unknown way, but I have made it work so far.

If anyone knows more about com.android.systemui, what it really does and if it is safe to disable it, please comment!

Also see my post: Safe to disable com.android.systemui?

where I hope to get some answers...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top