Android即时通信设计与实现(代码)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Android即时通信系统设计与实现(代码如下):1,登录界面(IP地址):
package Nclient;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditT ext;
public class LoginActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(yout.login);
Button btnLink = (Button) findViewById(R.id.btnLink);
final EditT ext etIPaddr = (EditT ext) findViewById(R.id.etIPaddr);
final Intent intent = new Intent();
btnLink.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
intent.setClass(LoginActivity.this, ClientActivity.class);
//清除堆栈中其他Activity
intent.putExtra("IPaddr", etIPaddr.getT ext().toString());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
LoginActivity.this.finish();
startActivity(intent);
}
});
}
}
2,客户端(Android):
package Nclient;
// 123456
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import .Socket;
import .UnknownHostException;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener; import android.widget.Button;
import android.widget.EditT ext;
import android.widget.T oast;
public class ClientActivity extends Activity {
Socket socket;
BufferedReader br;
PrintStream ps;
Button btnSend;
Button btnExit;
Button btnFile;
EditT ext etSend;
EditT ext etReceive;
MyHandler handle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.client);
Intent intent = getIntent();
String IPaddr = new String(intent.getStringExtra("IPaddr"));
etReceive = (EditT ext) findViewById(R.id.etReceive);
etSend = (EditT ext) findViewById(R.id.etSend);
btnSend = (Button) findViewById(R.id.btnSend);
btnExit = (Button) findViewById(R.id.btnExit);
btnFile=(Button)findViewById(R.id.btnFile);
try
{
socket = new Socket(IPaddr, 9998);
if (socket != null)
T oast.makeT ext(ClientActivity.this, "成功连接到服务端", T oast.LENGTH_LONG)
.show();
// 创建输入流
InputStream ins = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(ins, "gbk");
br = new BufferedReader(isr);
// 创建输出流
OutputStream os = socket.getOutputStream();
ps = new PrintStream(os);
btnSend.setOnClickListener(new RcbtnOnclickListener());
handle = new MyHandler();
new Thread(new Receive()).start();
// handle.post(new Receive());
} catch (UnknownHostException e)
{
e.printStackT race();
} catch (IOException e)
{
e.printStackT race();
}
btnFile.setOnClickListener(new OnClickListener() {
public void onClick(View v)// 为文件按钮注册按钮监听
{
System.out.println("benfile");
}
});
btnExit.setOnClickListener(new OnClickListener() {
public void onClick(View v)// 为退出按钮注册按钮监听