This article describes the Android programming implementation of simple UDP Client. Share to everyone for your reference, specific as follows:
The code is debugged inside the 4.2.2 through
1. Remember to add permission
<uses-permission android:name= "Android.permission.INTERNET"/>
Note: After Android 4.0, you can't communicate with the main thread, or you will throw an exception.
2. Code
Mainactivity.java:
Package mao.example.quicksend;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.Button;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import Android.view.View; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {super.
OnCreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initui ();
Get button Object button Btnconnect = (button) Findviewbyid (R.id.buttonconnect);
Btnconnect.setonclicklistener (New Button.onclicklistener () {public void OnClick (View v) {//) handling events here
ConnectServer ();
UdpClient Udpet = new UdpClient ("192.168.0.14", "123");
Udpet.start ();
Displaytoast ("click" \ "connect \" button);
}
}); Send button Btnconnect = (button) Findviewbyid (r.id.buttonsEnd);
Btnconnect.setonclicklistener (New Button.onclicklistener () {public void OnClick (View v) {//) handling events here
SendText ();
}
}); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar
If it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true; @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, so long//As you specify a parent Activ
ity in Androidmanifest.xml.
int id = item.getitemid ();
if (id = = r.id.action_settings) {return true;
return super.onoptionsitemselected (item);
/* Displays Toast/public void Displaytoast (String str) {toast.maketext (this, str, toast.length_short). Show ();
public void Initui () {TextView text= (TextView) Findviewbyid (R.id.edittextip); Text. SetText ("192.168.0.14");
Text = (TextView) Findviewbyid (R.id.edittextfilepath);
Text.settext ("UDP Client Send Test");
//Connect server public void SendText () {TextView editip= (TextView) Findviewbyid (R.id.edittextip);
TextView edittext= (TextView) Findviewbyid (R.id.edittextfilepath);
String message = Edittext.gettext (). toString () + "\ r \ n";
UdpClient Udpet = new UdpClient (Editip.gettext (). toString (), message);
Udpet.start ();
}
}
Udpclient.java:
Package mao.example.quicksend;
Import java.io.IOException;
Import Java.net.DatagramPacket;
Import Java.net.DatagramSocket;
Import java.net.InetAddress;
Import java.net.SocketException;
Import java.lang.String;
The public class UdpClient extends thread{the public String M_serverip;
Public String M_text;
Public UdpClient (String strServerIP, String strText) {M_serverip = strServerIP;
M_text = StrText;
@Override public void Run () {//TODO auto-generated method stub int TIMEOUT = 3000;
int servport = 8800;
byte[] Bytestosend = M_text.getbytes ();//"Test_client". GetBytes ();
try {inetaddress serveraddress = Inetaddress.getbyname (M_serverip);
Datagramsocket socket = new Datagramsocket ();
Socket.setsotimeout (TIMEOUT);
Datagrampacket sendpacket = new Datagrampacket (bytestosend,bytestosend.length,serveraddress,servport);
Socket.send (Sendpacket);
Socket.close ();
catch (SocketException e) {e.printstacktrace (); }catch (IOException e) {e.printstacktrace ();
}
}
}
3. Activity_main.xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:id=" @+id/linearlayout1 "android:layout_width=" Match_parent "Android:layout_"
height= "match_parent" android:orientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin
"Android:paddingtop=" @dimen/activity_vertical_margin "tools:context=" mao.example.quicksend.MainActivity "> <edittext android:id= "@+id/edittextip" android:layout_width= match_parent "android:layout_height=" Wrap_cont Ent "android:hint=" @string/text_ip "/> <button android:id=" @+id/buttonconnect "Android:layout_width=" M Atch_parent "android:layout_height=" wrap_content "android:text=" @string/text_connect "/> <EditText and
Roid:id= "@+id/edittextfilepath" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:hint= "@string/text_filepath" > <requestfocus/> </Edi ttext> <button android:id= "@+id/buttonfilesel" android:layout_width= "Match_parent" Android:layout_heig ht= "Wrap_content" android:text= "@string/text_sel"/> <button android:id= "@+id/buttonsend" Android:layo Ut_width= "Match_parent" android:layout_height= "wrap_content" android:text= "@string/text_send"/> </LinearLay
Out>
For more information on Android-related content readers can view the site topics: "The Android Communication method Summary", "Android Debugging techniques and common problem solving method summary", "Android Development introduction and Advanced Course", "Android Multimedia operation tips Summary (audio, Video, audio, etc.), "Summary of Android Basic components usage", "Android View Overview", "Android Layout layout skills Summary" and "Android Control usage Summary"
I hope this article will help you with the Android program.