How to disable home button in Android like lock screen apps do?

后端 未结 6 1747
后悔当初
后悔当初 2020-12-24 14:43

I know this question is asked many times but I found that none of the solution is working. I tried the code given below...

   protected void onPause() {
            


        
6条回答
  •  -上瘾入骨i
    2020-12-24 14:53

    I have done a lot of research to design a lock screen and finally found a solution. Android disabled the feature to override System bars except the back button. But there is a little work around to make this work:

    Understand and implement screen pinning patiently and you will be successful.

    You can create an app to control what all applications you want to implement screen pinning in or you can implement screen pinning directly in the same application you want to pin.

    I'm going to show you the later implementation in this article:

    1. Firstly your app should be the device owner.

    You can do it in several ways and the easiest is to execute the command:

    adb shell dpm set-device-owner [yourPackageName]/.[MyDeviceAdminReceiver]

    Create a receiver(MyDeviceAdminReceiver) that extends DeviceAdminReceiver. You needn't have any code in here. For more info on Device owner implementation refer this link
    http://florent-dupont.blogspot.com/2015/02/10-things-to-know-about-device-owner.html

    Register the receiver in the AndroidManifest.xml file this way :

    
         
    
           
             
           
      
    

    2. Your onCreate method should look like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock_screen);
    
        ComponentName deviceAdmin = new ComponentName(this, MyDeviceAdminReceiver.class);
        DevicePolicyManager mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    
    
        if (mDpm.isDeviceOwnerApp(getPackageName())) {
            mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()});
        }
    
        if (mDpm.isLockTaskPermitted(this.getPackageName()))
            startLockTask();
    

    3.To unpin the screen and make Navigation Bar functional:

    Call the function stopLockTask() at a place in your code where you want to unpin. For example in my application, as soon as I verify that the user has typed the correct passcode, I call this function:

     if (userInput.length() == 4) {
    
                        if (userInput.equals(passcode)) {
                            userInput = "";
                            etxtPasscodeDisplay.setText("");
                            stopLockTask(); // this is what you need
                            unlockHomeButton(); // A method to show home screen when 
                             passcode is correct
                            finishAffinity(); //kill other activities
                        }
    

    Extra Info which usually is required for lockscreens:

    1. If your app is the first thing that comes up after boot:

    You need a service(StartAtBootService) and a receiver (BootCompletedReceiver) for this.

    2. If you want your app to show up after screen lock and unlock (the power button is pressed to lock and unlock):

    Create AEScreenOnOffService that extends service and AEScreenOnOffReceiver that extends BroadcastReceiver to launch your activity when the screen is on.

    For a detailed info on everything I mentioned here, refer http://www.sureshjoshi.com/mobile/android-kiosk-mode-without-root/
    This is an excellent write up which helped me a lot. Special thanks to the author.

    I need at least 10 reputation to post more than two links. As I'm new to stackoverflow I don't have enough reputation so I'm sorry for not being able to share all the links I referred. Will surely update the post once I get access.

提交回复
热议问题