Refresh TextView on volume up key pressed

I am new to android, help me find a solution for this question.

I need to update the TextView of my android app, when the user presses the volume up button, every time the user presses the button, the count should increase like 1,2,3,4,5 ... for that i did the following:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
            switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                if (action == KeyEvent.ACTION_DOWN) {

                    int current =  Integer.parseInt((String)counter_view.getText());
                    counter_view.setText(current+1+"");

                }
                return true;
            default:
                return super.dispatchKeyEvent(event);
            }
        }

      

This code works, but the problem with it is the update in text mode is very slow, if I press the button continuously then it takes little time to update the textbox. Is there any other way to do this?

+2


source to share


3 answers


Try updating your text view inside runOnUiThread ():



@Override
public boolean dispatchKeyEvent(KeyEvent event) {
   switch (event.getKeyCode()) {
     case KeyEvent.KEYCODE_VOLUME_UP:
          if (event.getAction() == KeyEvent.ACTION_DOWN) {
              runOnUiThread(new Runnable() {
                 @Override
                 public void run() {
                   int current =  Integer.parseInt((String)counter_view.getText());
                   counter_view.setText(current+1+"");        
                 }
              });
          }
          return true;
     default:
          return super.dispatchKeyEvent(event);
    }
 }

      

0


source


Try something like:



@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_UP) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int current = Integer.parseInt((String) counter_view.getText());
                        counter_view.setText(current + 1);        
                    }
                });
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }
}

      

0


source


@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(KEYCODE_VOLUME_UP==keyCode)
        {
            //do what you want to do here
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

      

In this case, your key event will be detected very quickly, and you can also quickly change the value of the text box. just put the volume keycode up in KEYCODE_VOLUME_UP, so every time you press that key than the value of your textbox will not change and your volume app function will not run at that time because we are returning true instead of the parent class event .

0


source







All Articles