2013年8月10日星期六

android thread and handle communication?

I want to achieve, press the button to generate a thread, and then thread right information to the handle, the handle is set textview display content, but I found that I can not set the display content textview not know where the wrong, neighborhoods ~ ~ My code is very simple, I think it should be no problem:

package com.FJICC.lzm;
import .....
public class tcp_server extends Activity{

private Button serverStart_btn;
private Button serverStop_btn;
private TextView receivedata_tv;
private Button setport_btn;
private EditText senddata_et;
private Button send_btn;
public int PORT = 8080;
public Handler mHandler;
protected static final int GUINOTIFIER = 0x1234;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tcpserver_main);

serverStart_btn=(Button)findViewById(R.id.btnStart);
serverStop_btn=(Button)findViewById(R.id.btnStop);
setport_btn=(Button)findViewById(R.id.btnSet);
send_btn=(Button)findViewById(R.id.btnSend);

senddata_et=(EditText)findViewById(R.id.et_send);
receivedata_tv=(TextView)findViewById(R.id.tv_receive);

serverStart_btn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
serverStart_btn.setEnabled(false);
setport_btn.setEnabled(false);
serverStop_btn.setEnabled(true);
new Thread()
{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
    Message m = new Message();
                    m.what = tcp_server.GUINOTIFIER;
                    tcp_server.this.mHandler.sendMessage(m);                    
}catch(IOException e){
e.printStackTrace();
}
}
}.start();
}
});

mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case tcp_server.GUINOTIFIER: 
                    tcp_server.this.receivedata_tv.setText("xxxxxxx");
                        break;
                }
                super.handleMessage(msg);
            }
        };
}

}



------ Solution ------------------------------------- -------


not: the thread which can not handle things too complicated.
but which do not deal with network transceiver thread too complicated, network transceiver thread only send and receive data. Data processing by other threads to do it.


------ For reference only ---------------------------------- -----
Handler not declared as a member variable to try
Handler mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case tcp_server.GUINOTIFIER: 
                        tcp_server.this.receivedata_tv.setText("xxxxxxx");
                        break;
                }
                super.handleMessage(msg);
            }
        };

------ For reference only ----------------------------------- ----
tcp_server.this. Do not add the class inside this thing looks weird

new Thread () in front of a row after you click Add log to see whether there is a response here

public void handleMessage (Message msg) {
here to add log to see if the execution and printing msg.what

------ For reference only ---------------------------------- -----



I put some unnecessary stuff all deleted, but later found they can, but I added the amount of the content and then die · · I see some people say: inside the thread can not handle things too complicated. My thread is receiving socket and then, trying to show the received data into textview years, this "show features" I am what I want to handle the reason, it seems a little visualization. . . . .
My code is such a change amount:

serverStart_btn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
serverStart_btn.setEnabled(false);
setport_btn.setEnabled(false);
serverStop_btn.setEnabled(true);

new Thread()
{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
ServerSocket serverSocket=null;
try{
//创建ServerSocket对象监听PORT端口
serverSocket = new ServerSocket(PORT);
//接收tcp连接返回socket对象
Socket socket= serverSocket.accept();
//获得输入流
InputStream inputStream=socket.getInputStream();
//获得输出流
OutputStream outputStream = socket.getOutputStream();
byte []byteBuffer=new byte[1024];
int temp = 0;
//读取接收到的数据
while((temp = inputStream.read(byteBuffer))!=-1)
{
outputStream.write(byteBuffer, 0, temp);
}
//System.out.println(new String(byteBuffer,0,temp));
outputStream.flush();
socket.close();
serverSocket.close();

Message m = new Message();
                    m.what = tcp_server.GUINOTIFIER;
                    tcp_server.this.mHandler.sendMessage(m);
                                       
}catch(IOException e){
e.printStackTrace();
}
}
}.start();
}
});

mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case tcp_server.GUINOTIFIER://tcp_server是Activity的类名
                        //得到Handle的通知了 这个时候你可以做相应的操作
                    receivedata_tv.setText("xxxxxxx");
                        break;
                }
                super.handleMessage(msg);
            }
        };

In the two are: onCreate (Bundle savedInstanceState) {.......} inside
Thank you enlighten me ah!
------ For reference only -------------------------------------- -
you this network ah, estimated long lead time for reception, try to see added time log
serverSocket = new ServerSocket (PORT);
/ / receive tcp connection socket object returned
Socket socket = serverSocket.accept ();
/ / get input stream
InputStream inputStream = socket.getInputStream ();
/ / get the output stream
OutputStream outputStream = socket.getOutputStream ();
byte [] byteBuffer = new byte [1024];
int temp = 0;
/ / read the received data
while ((temp = inputStream.read (byteBuffer))! = -1)
{
outputStream.write (byteBuffer, 0, temp);
}
/ / System.out.println (new String (byteBuffer, 0, temp));
outputStream.flush ();
socket.close ();
serverSocket.close ();

look at this code how long it took to complete

thread can do a lot of work, but can not do UI work.

Message m = new Message ();
m.what = tcp_server.GUINOTIFIER;
tcp_server.this.mHandler.sendMessage (m)

the first to write in the phrase
ServerSocket serverSocket = null;
try {
while loop before I put that out a long time.
------ For reference only -------------------------------------- -

can, I just changed the location under the handle, I put

Message m = new Message();
m.what = tcp_server.GUINOTIFIER;
tcp_server.this.mHandler.sendMessage(m)

placed that while receiving the inside, you can, ah ~ ~ ~

while((temp = inputStream.read(byteBuffer))!=-1)
{
outputStream.write(byteBuffer, 0, temp);
Message m = new Message();
m.what = tcp_server.GUINOTIFIER;
tcp_server.this.mHandler.sendMessage(m)
}

Thank you ~ ~ ~ ~
------ For reference only ---------------------------------------
points to the wrong · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
------ For reference only ---------------------------------------


Haha okay enough to solve the

没有评论:

发表评论