Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement handler in another class?

In my app I have three buttons and when one button is clicked it calls a thread to start thing is i want to be able to input in edittext string in to the threads and the do some work to it then have it returned to the UI Thread where i can display it or put it into an opengl to display an Object. I've read up on Handles and im not sure i fully understand them and maybe if anyone knows a way to make my own handler code. Also I've read up on Async and I dont think it would benifit my app.(personal opion if it would benifit my app let me know) My question is how would i get the info from UI edittext when enter is pressed to the line thread in DrawingUtils class then work is done to it then it comes back to the UI to be ethier displayed or inputed in an openGl program?

Here is MainActivity class:

  public class MainActivity extends Activity implements OnClickListener {
    EditText cl;
    TextView info;
    Button enter;
    Button line;
    Button arc;
    Line callLine = new DrawingUtils.Line();
    Enter callEnter = new DrawingUtils.Enter();
    Arc callArc = new DrawingUtils.Arc();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        info = (TextView) findViewById(R.id.info);
        enter = (Button) findViewById(R.id.enter);
        line = (Button) findViewById(R.id.line);
        arc = (Button) findViewById(R.id.arc); 

        Handler UIhandler = new Handler() {
              @Override
              public void handleMessage(Message msg) {
                  Bundle bundle = msg.getData();
                    String string = bundle.getString("myKey");

                 }
             };

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.enter:
             callEnter.start();
            break;
        case R.id.line:
            callLine.start();
            break;
        case R.id.arc:
            callArc.start();
            break;
        }

    };

}

Here is DrawingUtils class:

    public class DrawingUtils {
    MainActivity handle = new MainActivity();

    // Thread classes for buttons
    public static class Enter extends Thread {
        Thread enter = new Thread() {
            public void run() {

            }

        };

        public static class Line extends Thread {
            Thread line = new Thread() {
                public void run() {

                }
            };

        }

        public static class Arc extends Thread {
            Thread arc = new Thread() {
                public void run() {

                }
            };
        }
    }
}
like image 492
Dakota Miller Avatar asked Jun 22 '13 04:06

Dakota Miller


Video Answer


1 Answers

public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstance) {
        // ....
        Handler myHandler = new Handler() {
            @Override
            public void handleMessage (Message msg) {
                doCoolStuffWhenMessageReceived();
            }
        }
        MySecondClass secondClass = new MySecondClass(myHandler);
        // ....
    }
}


public class MySecondClass {
    private handler;
    public MySecondClass(Handler handler){
        this.handler = handler;
    }

    private void someMethodToCallActivity() {
        handler.sendEmptyMessage(0);
    }

}
like image 75
KOTIOS Avatar answered Oct 18 '22 08:10

KOTIOS