在Android 4.x中禁用主页按钮

时间:2015-03-04 08:45:39

标签: android android-4.0-ice-cream-sandwich android-homebutton

在我为孩子们开发应用程序时,我需要禁用主页按钮,以便他们不会在课堂上关闭应用程序。 有人知道如何在android 4.X中禁用它吗? (主页按钮是真实按钮,但屏幕上没有虚拟按钮)

2 个答案:

答案 0 :(得分:0)

你不能 根据{{​​3}},主页按钮不会传递给应用程序并由框架处理。

答案 1 :(得分:0)

您可以在 rooted 设备中执行此操作。

这就是我为此所做的:

package com.juanjovega.examples;

import android.app.Activity;
import android.os.Build;
import android.util.Log;

public class KioskActivity extends Activity {

    @Override
    public void onResume() {
        super.onResume();
        // Work around an Android 2.3 bug that sometimes causes us to become
        // invisible after turning the display off and on with the power button.
        setVisible(false);
        setVisible(true);
    }

    public static void setFullScreen(boolean fullscreen) {
        Process proc = null;

        String ProcID = "79"; // HONEYCOMB AND OLDER

        if (Build.VERSION.SDK_INT >= 14) {
            ProcID = "42"; // ICS AND NEWER
        }

        try {
            if (fullscreen) {
                proc = Runtime.getRuntime().exec("su -c service call activity " + ProcID + " s16 com.android.systemui");
                Log.d("kiosk", "Fullscreen mode ON");
            } else {
                Runtime.getRuntime().exec("am startservice --user 0 -n com.android.systemui/.SystemUIService");
                Log.d("kiosk", "Fullscreen mode OFF");
            }
        } catch (Exception ex) {
        }
        try {
            proc.waitFor();
        } catch (Exception ex) {
        }
    }

    // Override the default Back key action so that we don't lose control.
    @Override
    public void onBackPressed() {
        Log.d("kiosk", "onBackPressed");
    }

    public void clearHomeConfig() {
        Log.d("kiosk", "clearHomeConfig");

        getPackageManager().clearPackagePreferredActivities(getPackageName());
    }

    public void exitApp() {
        // Restore UI.
        setFullScreen(false);

        // Clear home config.
        getPackageManager().clearPackagePreferredActivities(getPackageName());

        // Exit
        System.exit(0);
    }
}