繁体   English   中英

Android中的类间通信

[英]Inter-class communication in Android

我有一个名为TCPClient的类(在Android中), TCPClient从服务器接收字符串。 我需要发送给函数printmsgShowmsg ,这是在同一个包中TCPClient
以下代码不起作用。

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

Showmsg类中:

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

由于TCPClientRunnable ,因此我在给定代码中没有看到的是TCPClient .start() 另外,我不知道您的toast(str)方法如何工作,但不要忘记.show() 此代码应运行。

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);
    }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM