user1944690
user1944690

Reputation: 9

Inter-class communication in Android

I have a class (in Android) called TCPClient which receives a string from the server. I need to send this to a function printmsg in class Showmsg, which is in the same package as TCPClient.
The following code doesn't work.

public class TCPClient implements Runnable {  
    ..   
    ..  
    public void run() {  
        ..  
        Showmsg obj = new Showmsg();  
        obj.printmsg("Hello");  
    }  
}  

In class Showmsg:

public void printmsg(String str) {    
    Toast( . . );    
}

Upvotes: 0

Views: 360

Answers (1)

stealthjong
stealthjong

Reputation: 11093

What I did not see in the given code is .start(), since TCPClient is Runnable. Plus I dont know how your toast(str) method works, but dont forget .show(). This code should run.

public class TCPClient implements Runnable {  
    public void run() {  
        Showmsg obj = new Showmsg();  
        obj.printmsg("Hello");  
    }  
}  

public class MyActivity {
    TCPClient tcp;

    public void onCreate(Bundle b) {
        super.onCreate(b);
        tcp = new TCPClient();
    }

    public void onResume() {
        super.onResume();
        tcp.start();
    }

}

public class Showmsg {
    public void printmsg(String str) {    
        toast(str);
    }

    private void toast(String str) {
        Log.d(TAG, str);
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        System.out.println(str);
    }
} 

Upvotes: 1

Related Questions