Android 채팅 소스 05

Programming/Android 2013. 8. 21. 15:56 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

package com.tistory.tansanc.Test130805;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	String msg;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SharedPreferences pref = getSharedPreferences("Chatting", 0);
		String nickName = pref.getString("nickName", "noname");
		if (nickName.equals("noname")) {
			Intent intent = new Intent(this, InputNickNameActivity.class);
			startActivity(intent);
			// 최초 구동 시
		}
		setContentView(R.layout.activity_main);
		MessageReceiveThread mrt = new MessageReceiveThread();
		mrt.start();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub

		SharedPreferences pref = getSharedPreferences("Chatting", 0);
		String nickName = pref.getString("nickName", "noname");
		super.onResume();
	}

	public void mOnClick(View v) {
		EditText editText1 = (EditText) findViewById(R.id.editText1);
		msg = editText1.getText().toString();
		MessageSendThread mst = new MessageSendThread();
		mst.start();
	}

	public void MessageBoxPopUp(String message, String positive, String negative) {
		new AlertDialog.Builder(this).setTitle("질문").setMessage(message)
				.setPositiveButton(positive, mClick)
				.setNegativeButton(negative, mClick).show();
	}

	DialogInterface.OnClickListener mClick = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			if (whichButton == DialogInterface.BUTTON1) {
				msg = "OK";
				MessageSendThread mst = new MessageSendThread();
				mst.start();
			} else {
				msg = "NO";
				MessageSendThread mst = new MessageSendThread();
				mst.start();
			}
		}
	};

	class MessageReceiveThread extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub

			Socket socket = null;
			PrintWriter out = null;
			BufferedReader in = null;

			try {
				socket = new Socket("115.20.247.141", 2013);
				out = new PrintWriter(new OutputStreamWriter(
						socket.getOutputStream(), "utf-8"), true);
				in = new BufferedReader(new InputStreamReader(
						socket.getInputStream(), "utf-8"));

				while (true) {
					String receiveString = in.readLine();
					Message message = new Message();
					message.obj = receiveString;
					mHandler.sendMessage(message);
				}
			} catch (UnknownHostException e) {
			} catch (IOException e) {
			}

			super.run();
		}
	}

	class MessageSendThread extends Thread {
		public void run() {
			Socket socket = null;
			PrintWriter out = null;
			BufferedReader in = null;

			try {
				socket = new Socket("115.20.247.141", 2013);
				out = new PrintWriter(new OutputStreamWriter(
						socket.getOutputStream(), "utf-8"), true);
				in = new BufferedReader(new InputStreamReader(
						socket.getInputStream(), "utf-8"));

				out.println(msg);

				out.close();
				in.close();
				socket.close();
			} catch (UnknownHostException e) {
			} catch (IOException e) {
			}
		}
	}

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			String receiveString = (String) msg.obj;
			if (receiveString.charAt(0) != 'q' ) {
				TextView textView1 = (TextView) findViewById(R.id.textView1);
				textView1.setText(textView1.getText() + "\n" + receiveString);
			}
			else
			{
				StringTokenizer st = new StringTokenizer(receiveString,":");				
				st.nextToken();
				MessageBoxPopUp(st.nextToken(),st.nextToken(),st.nextToken());
			}
		}
	};
}



'Programming > Android' 카테고리의 다른 글

Android 실습 0823  (0) 2013.08.23
PinchToZoom ImageView 로 구현  (0) 2013.08.23
Android 채팅 소스 04  (0) 2013.08.21
Android 채팅 서버 소스 01  (0) 2013.08.20
Android 채팅 소스 03  (0) 2013.08.20