1

i put a couple of onclick methods inside of a run() thread method in a sockets program for android.

they don't seem to work. is possible to do put these onclick listeners inside of the run() method or it it now possible. not sure why the buttons are not generating a response when clicking on them. the message is not getting sent

here is the two onclick listeners that i am using. one is sendA and the other is sendB and they send the text messages testA and testB

public class ClientThread implements Runnable {

    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Log.d("ClientActivity", "C: Connecting...");
            Socket socket = new Socket(serverAddr, 8080);
            connected = true;
            while (connected) {
                try {
                    Log.d("ClientActivity", "C: Sending command.");
                    final PrintWriter out = new PrintWriter(
                        new BufferedWriter(new OutputStreamWriter(
                            socket.getOutputStream())), true);
                    sendA.setOnClickListener(new View.OnClickListener() {

                        // ananamous inner class override for on click
                        public void onClick(View v) {
                            out.println("testA");
                        }
                    });
                    sendB.setOnClickListener(new View.OnClickListener() {

                        // ananamous inner class override for on click
                        public void onClick(View v) {
                            out.println("testB");
                        }
                    });
                    // where you issue the commands
                    // out.println("testX");
                    Log.d("ClientActivity", "C: Sent.");
                } catch (Exception e) {
                    Log.e("ClientActivity", "S: Error", e);
                }
            }
            socket.close();
            Log.d("ClientActivity", "C: Closed.");
        } catch (Exception e) {
            Log.e("ClientActivity", "C: Error", e);
            connected = false;
        }
    }
}
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Kevik
  • 9,181
  • 19
  • 92
  • 148
  • This suggests you cannot http://stackoverflow.com/questions/3285371/android-is-view-onclick-method-invoked-on-main-ui-thread – anthropomo Jan 24 '13 at 02:25

2 Answers2

1

I make a simple demo and it works;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HelpActivity" >

<Button
    android:id="@+id/a"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="A" />
<Button
    android:id="@+id/b"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/a"
    android:text="B" />

</RelativeLayout>

public class HelpActivity extends Activity {

private Button aBtn;
private Button bBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_help);
    aBtn = (Button) findViewById(R.id.a);
    bBtn = (Button) findViewById(R.id.b);
    new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            aBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.v("a", "a");
                }
            });
            bBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {   
                    Log.v("b", "b");
                }
            });

        }
    }).start();
}

}

when I click the button ,there are logs in LogCat! LogCat View

So,maybe your situation is caused by sequence.I mean when you click the button the listener hasn't been attached to the button.

Well,if you insist set listener on main thread ,you can use handler This Handler class should be static or leaks might occur: IncomingHandler

Community
  • 1
  • 1
fearless
  • 98
  • 8
  • actually mine works now too. no problems. it was a small typing error in my code on the sever side. so it appears that you actually can put onclick listeners inside of the run() method – Kevik Jan 24 '13 at 03:01
  • now when i click on the A button it displays testA on the server android tablet and when i click the B button it changes the message to testB. everything is working perfectly. – Kevik Jan 24 '13 at 03:03
  • only one problem is when after i connect the tablets via wifi with the sockets code. if i tilt the tablet from landscape to portrait orientation. that orientation change stops the socket connection. so i have to re connect. so my idea is to put all the code in a service instead of an activity so any screen orientation change will not kill the connection. – Kevik Jan 24 '13 at 03:05
  • I remember I have read some article about maintain things while configuration changing without using the Service component.Let me check. – fearless Jan 24 '13 at 03:14
  • @Kevik I find it.It is about maintain an AsyncTask during rotation,I think it will give you a clue to solve your problem without using service.If it is convenient, you can find it in "Beginning Android 3(4)" by Apress in Chapter 20 subtile "Threads and Rotation". – fearless Jan 24 '13 at 03:28
1

yes you can just but the onclick listeners inside a runnable thread again

it works for me like this

 public class sendLogs implements Runnable{ 
    @Override
    public void run() {


    //your other codes



    runOnUiThread(new Runnable() {
     @Override
     public void run() {
    //place your onclick listener method here...
    }
    }
She Smile GM
  • 1,322
  • 1
  • 11
  • 33