0

I am new in android. I am creating a Lock screen application. In my application, I want to disable all the outside keys like Home key, Back key.. I already disabled the Back key using:

@Override
public void onBackPressed() {
    return;
    // Do nothing!
}

But i referred a lot of sites and questions in Stack Overflow to disable the Home key in my app. But nothing worked. My App working on API 16 .. Please help me. Any help would be appreciated.

Thanks a lot in advence

Ziem
  • 6,579
  • 8
  • 53
  • 86
Abin Thaha
  • 4,493
  • 3
  • 13
  • 41
  • May this help you http://stackoverflow.com/questions/10025660/override-home-and-back-button-is-case-a-boolean-is-true/10025904#10025904 – Apurva Mar 01 '15 at 15:48

1 Answers1

1

I recommend reading: How-To Create a Working Kiosk Mode in Android

Disable the home button and detect when new applications are opened

Since Android 4 there is no effective method to deactivate the home button. That is the reason why we need another little hack. In general the idea is to detect when a new application is in foreground and restart your activity immediately.

At first create a class called KioskService that extends Service and add the following snippet:

public class KioskService extends Service {
    private static final long INTERVAL = TimeUnit.SECONDS.toMillis(2); // periodic interval to check in seconds -> 2 seconds
    private static final String TAG = KioskService.class.getSimpleName();
    private static final String PREF_KIOSK_MODE = "pref_kiosk_mode";

    private Thread t = null;
    private Context ctx = null;
    private boolean running = false;

    @Override
    public void onDestroy() {
        Log.i(TAG, "Stopping service 'KioskService'");
        running =false;
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "Starting service 'KioskService'");
        running = true;
        ctx = this;
        
        // start a thread that periodically checks if your app is in the foreground
        t = new Thread(new Runnable() {
            @Override
            public void run() {
                do {
                    handleKioskMode();
                    try {
                        Thread.sleep(INTERVAL);
                    } catch (InterruptedException e) {
                        Log.i(TAG, "Thread interrupted: 'KioskService'");
                    }
                }while(running);
                stopSelf();
            }
        });

        t.start();
        return Service.START_NOT_STICKY;
    }

    private void handleKioskMode() {
        // is Kiosk Mode active? 
            if(isKioskModeActive()) {
                // is App in background?
            if(isInBackground()) {
                restoreApp(); // restore!
            }
        }
    }

    private boolean isInBackground() {
        ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        return (!ctx.getApplicationContext().getPackageName().equals(componentInfo.getPackageName()));
    }

    private void restoreApp() {
        // Restart activity
        Intent i = new Intent(ctx, MyActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ctx.startActivity(i);
    }
    
    public boolean isKioskModeActive(final Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getBoolean(PREF_KIOSK_MODE, false);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Add the following method in your AppContext class to start the service via application context creation.

@Override 
public void onCreate() {   
    super.onCreate();   
    instance = this;   
    registerKioskModeScreenOffReceiver();   
    startKioskService();  // add this 
}

private void startKioskService() { // ... and this method  
    startService(new Intent(this, KioskService.class)); 
}

Last, add the service declaration and the permission for retrieving the foreground process to the manifest:

<service android:name=".KioskService" android:exported="false"/>

<uses-permission android:name="android.permission.GET_TASKS"/>

Basically, the thread checks every two seconds if your application is running in foreground. If not, the thread will immediately recreate your activity.

Ziem
  • 6,579
  • 8
  • 53
  • 86
  • Thank you for this! Just a note, GET_TASKS is not needed since it's checking about tasks internal to the app or something (I just removed it and is working on Android 5.1 and 8.1). – Edw590 Jul 01 '21 at 17:38