Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
798 views
in Technique[技术] by (71.8m points)

how to disable home button in android?

i want to disable android device home button when my app runs.

i have tried following code but it din't help :

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
            Toast.makeText(this, "You pressed the home button!",
                    Toast.LENGTH_LONG).show();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

     @Override
     protected void onNewIntent(Intent intent) {
     super.onNewIntent(intent);
     if (Intent.ACTION_MAIN.equals(intent.getAction())) {
     Log.i("MyLauncher", "onNewIntent: HOME Key");

     }
     }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this can be done in another style if suits your requirement. By creating your activity as home activity. If you want to disable home button and show your custom application activity as launcher when home button is pressed. Just add these lines in manifest for that activity for which you want your launcher.

<activity
            android:name="com.example.TempActivity"
            android:clearTaskOnLaunch="true"
            android:excludeFromRecents="true"
            android:launchMode="singleTask"
            android:screenOrientation="landscape"
            android:stateNotNeeded="true" >
             <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>
</activity>

After adding and run your application. if user presses the home button, Android will ask for which launcher you want your home. Then your have to select your application launcher ALWAYS not ONLY ONCE.

if you want fully disable the user, so that he cant move to another screen then set theme to fullscreen with NoTitlebar.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...