Charbel Wakim
Charbel Wakim

Reputation: 146

Disable app from closing Android

I need to create an app that cannot be closed on Android, neither by the home button or the back button. Currently I am looking into creating my own ROM as the app does not need to be published it is an internal app for my company, but was thinking if there is any other easier options.

Upvotes: 1

Views: 243

Answers (4)

StormSpirite
StormSpirite

Reputation: 110

Be careful when you try this.
To disable back button just override the onBackPressed method:

@Override
public void onBackPressed() {
   // super.onBackPressed();
}

To disable the home button try this

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        Log.i("HOMEBUTTON", "Home Button PRESSED");
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: 0

Rachit Mishra
Rachit Mishra

Reputation: 6112

As mentioned by @Commonsware, you need to make your application a launcher (Homescreen).

Also, You can keep a service running which every half milliseconds check which activity is on top, if it is any other activity, other than yours, then it pushes your activity to top.

You can use the PackageManager to get the current top activity.

Also, the back button press can be easily handled, in your app, just remove the super call in onBackPressed

Upvotes: 2

10bit
10bit

Reputation: 83

Here is one way I found to prevant the back button from closing, you might be able to do the same for the home button.

/* Prevent app from being killed on back */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // Back?
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Back
            moveTaskToBack(true);
            return true;
        }
        else {
            // Return
            return super.onKeyDown(keyCode, event);
        }
    }

I got this code from a similar question found here Prevent Back button from closing my application

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006914

Make your app be the home screen.

Upvotes: 3

Related Questions