Override home and back button is case a boolean is true

别说谁变了你拦得住时间么 提交于 2019-11-26 08:01:40

I was wondering if I can override the action of the back and home button is some cases.

Yes you can do override Home button.

I have developed an application which disable hard button, you can have a look. I have taken a toggle button which locks all hard button to work except Power button

public class DisableHardButton extends Activity {
    /** Called when the activity is first created. */
    TextView mTextView;
    ToggleButton mToggleButton;
    boolean isLock=false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTextView=(TextView) findViewById(R.id.tvInfo);
        mToggleButton=(ToggleButton) findViewById(R.id.btnLock);


        mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            isLock=isChecked;
            onAttachedToWindow();
        }
    });
   }
@Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) && isLock) {
            mTextView.setText("KEYCODE_HOME");
            return true;
        }
        else
            return super.dispatchKeyEvent(event);
    }
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if( (keyCode==KeyEvent.KEYCODE_BACK) && isLock)
        {
            mTextView.setText("KEYCODE_BACK");
            return true;
        }
           else
             return super.onKeyDown(keyCode, event);
    }
@Override
    public void onAttachedToWindow()
    {  
        System.out.println("Onactivity attached :"+isLock);
        if(isLock)
        {   
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);     
            super.onAttachedToWindow();
        }
        else
        {
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);     
            super.onAttachedToWindow();
        }
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ToggleButton
        android:id="@+id/btnLock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="UnLocked"
        android:textOn="Locked" />

</LinearLayout>

You call super.onBackPressed() to call the normal method. Exemple :

@Override
public void onBackPressed() {    
    if (activated) {
       //doyourthing
    } else {
       super.onBackPressed()
    }
}

No you can not. What you can do is to ovveride the method and manage the boolean inside it:

for instance:

 public void onBackPressed() {    // call my backbutton pressed method when boolean==true

      if (myCondition) {
          // take care of my needs
       } else 
            // call super to let the back behavior be "normal"

  }
Adil Soomro

Regarding overriding the behaviour of Home Button you are out of luck.

However if your app is a specific one and have limited targeted audience, like inter-organization app, hospital kiosk, restaurant ordering, you can try making your app as Home (the launcher app). You can find a good example here: How to Write Custom Launcher App in Android

And to override the back key events, lot of examples are there.

For example:

  1. Catch keypress with android
  2. Override back button to act like home button
  3. Android - How To Override the "Back" button so it doesn't Finish() my Activity?

make your boolean variable member variable

boolean temp;

@Override
        public void onBackPressed() {    // call my backbutton pressed method when boolean==true

if(temp)
//your methode
else
finish();
            }
Bigflow

I use this:

public void onBackPressed() {

        switch (screen) {
        case 1:
            screen = 99;
            setContentView(R.layout.menu);
            break;

        case 99:
            finish();
            break;

        }
        return;
    }

When I am in a other screen (other then menu screen), I set the variable screen to 1. When I press the back button, it goes back to the menu screen (instead of killing the app). and give the screen variable the number 99, then when you hit the back button again, it kills the app.

However, you can't change the home button.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!