public class MainActivity extends Activity {
public TextView tv;
public EditText nickText;
public EditText msgText;
public ScrollView sv;
public String nickName;
public String text;
public Button cB;
public Button eB;
public Button sB;
public Button rB;
public PrintWriter streamOut = null;
public BufferedReader streamIn = null;
public HubConnection conn;
public HubProxy proxy;
//public chatThread cThread = null;
public Thread mThread;
public Socket cSocket = null;
public Logger logger = new Logger() {
@Override
public void log(String message, LogLevel level) {
//tv.append(message+"\n");
sv.fullScroll(ScrollView.FOCUS_DOWN);
}
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_BACK){
tv.append("Back pressed"+"\n");
return true;
}
else if(keyCode==KeyEvent.KEYCODE_HOME){
tv.append("Home pressed"+"\n");
return true;
}
else if(keyCode==KeyEvent.KEYCODE_MENU){
tv.append("Menu pressed"+"\n");
return true;
}
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nickText = (EditText)findViewById(R.id.connText);
sv = (ScrollView)findViewById(R.id.scrollView1);
tv = (TextView)findViewById(R.id.text01); //sv안에 있음
msgText = (EditText)findViewById(R.id.chatText);
cB=(Button)findViewById(R.id.connBtn);
eB=(Button)findViewById(R.id.closeBtn);
sB=(Button)findViewById(R.id.sendBtn);
conn = new HubConnection("http://10.1.111.156:8080", "", true,logger);
proxy = conn.createHubProxy("MyHub");
proxy.subscribe(new Object() {
@SuppressWarnings("unused")
public void addMessage(String name, String message) {
tv.append(name + ": " + message+"\n");
tv.postInvalidate();
sv.fullScroll(ScrollView.FOCUS_DOWN);
sv.invalidate();
tv.invalidate();
Thread.interrupted();
}
});
conn.connected(new Runnable() {
@Override
public void run() {
proxy.invoke("addMessage","CONNECTED" );
}
});
Button sendBtn=(Button)findViewById(R.id.sendBtn);
sendBtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
String msgString = msgText.getText().toString();
proxy.invoke("addMessage",nickText.getText().toString(),msgText.getText().toString());
tv.postInvalidate();
sv.fullScroll(ScrollView.FOCUS_DOWN);
sv.invalidate();
tv.invalidate();
if (msgString != null && !"".equals(msgString)) {
msgText.setText("");
}
Thread.interrupted();
}
});
}
public void onDestroy() {
super.onDestroy();
tv.append("# [" + nickName + "]님이 나가셨습니다.");
}
public void connBtnClick(View v) {
switch(v.getId()){
case R.id.connBtn :
conn.start();
break;
case R.id.closeBtn :
conn.stop();
break;
}Thread.interrupted();
}
채팅 앱 만들었는데 화면전환을 하면 채팅 내용이 다 사라져요
그리고 채팅 내용도 바로바로 안뜨고.. 예를들어 텍스트 뷰를 한번 드래그 하거나 뒤로가기 버튼을 한번 눌러야 보낸 메세지가 떠요.