Android develops a chat room Client

Source: Internet
Author: User

Package com. xgl. Chat;

Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;
Import java. Io. printwriter;
Import java.net. Socket;
// Import java.net. inetaddress;

Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. content. dialoginterface;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. edittext;

Public class chatroomactivity extends activity {
 
/** Called when the activity is first created .*/
Private final string debug_tag = "chatroomactivity ";
// Private Static final string serverip = "192.168.1.4 ";
Private Static final int SERVERPORT = 54321;
Private thread mthread = NULL;
Private socket msocket = NULL;
Private button mbutton_in = NULL;
Private button mbutton_send = NULL;
Private edittext medittext01 = NULL;
Private edittext medittext02 = NULL;
Private bufferedreader mbufferedreader = NULL;
Private printwriter mprintwriter = NULL;
Private string mstrmsg = "";
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mbutton_in = (button) findviewbyid (R. Id. button_in );
Mbutton_send = (button) findviewbyid (R. Id. button_send );
Medittext01 = (edittext) findviewbyid (R. Id. edittext1 );
Medittext02 = (edittext) findviewbyid (R. Id. edittext2 );
// Login
System. Out. println ("=================== ");
Mbutton_in.setonclicklistener (New onclicklistener (){

@ Override
Public void onclick (view v ){
//

// Todo auto-generated method stub
Try {
// Inetaddress serveraddr = inetaddress. getbyname ("192.168.1.234 ");
// System. out. println (serveraddr. gethostaddress () + "============= IP =========== ");
// Connect to the server
Msocket = new socket ("192.168.1.7", 54321 );
System. Out. println ("the server is connected successfully! ");
Mbufferedreader = new bufferedreader (New inputstreamreader (msocket. getinputstream ()));
Mprintwriter = new printwriter (msocket. getoutputstream (), true );
} Catch (exception e ){
E. printstacktrace ();
Showdialog ("Logon exception:" + E. getmessage ());
System. Out. println (E. tostring () + "= ");
Log. E (debug_tag, E. tostring () + "++ ");
// Todo: handle exception
}
}
});


Mbutton_send.setonclicklistener (New onclicklistener (){

@ Override
Public void onclick (view v ){
System. Out. println ("========= 3 ================== ");
// Todo auto-generated method stub
Try {
// Obtain the information entered in the border
String STR = medittext02.gettext (). tostring () + "\ n ";
// Send it to the server
Mprintwriter. Print (STR );
Mprintwriter. Flush ();
} Catch (exception e ){
// E. printstacktrace ();
Log. E (debug_tag, E. tostring () + "--------");
}
}
});
Mthread = new thread ();
Mthread. Start ();
}/*
Private runnable mrunnable = new runnable (){
Public void run (){
While (true ){
Try {
If (mstrmsg = mbufferedreader. Readline ())! = NULL ){
// Line feed with caution
Mstrmsg + = "\ n ";
Mhandler. sendmessage (mhandler. obtainmessage ());
}
} Catch (exception e ){
// Todo: handle exception
Log. E (debug_tag, E. tostring ());
}
}
}
};
Handler mhandler = new handler (){
Public void handlemessage (Message MSG ){
Super. handlemessage (MSG );
// Refresh
Try {
Medittext01.append (mstrmsg );
} Catch (exception e ){
E. printstacktrace ();
Log. E (debug_tag, E. tostring ());
// Todo: handle exception
}
}
};*/
Public void showdialog (string MSG ){
New alertdialog. Builder (this). settitle ("prompt"). setmessage (MSG)
. Setpositivebutton ("OK", new dialoginterface. onclicklistener (){


Public void onclick (dialoginterface dialog, int which ){

// Todo auto-generated method stub

}

}). Show ();
}

}

 

 

 

 

 

 

 

 

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">

<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"

Android: text = "@ string/Hello" Android: maxlines = "10"/>

<Edittext
Android: Id = "@ + ID/edittext1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: EMS = "10"
Android: inputtype = "textpersonname" Android: linespacingextra = "12px" Android: singleline = "false">

<Requestfocus/>
</Edittext>
<Edittext
Android: Id = "@ + ID/edittext2"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: EMS = "10"
Android: inputtype = "textpersonname" Android: hint = "@ string/input"/>

<Button
Android: Id = "@ + ID/button_in"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "login"/>

<Button
Android: Id = "@ + ID/button_send"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "send"/>

</Linearlayout>

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Do not forget to add this sentence to chatmainfest. xml.

<Uses-Permission Android: Name = "android. Permission. Internet"/>

Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>

<String name = "hello"> Hello world, chatroomactivity! </String>
<String name = "app_name"> chatroom </string>
<String name = "input"> what do you want to say? </String>

</Resources>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.