This article describes the simple UDP Client programming implementation of Android. Share it for your reference, as follows:
The code is debugged in 4.2.2
1. Remember to add permissions
<uses-permission android:name=""/>
Note: After Android 4.0, socket communication cannot be performed on the main thread, otherwise exceptions will be thrown.
2. Code
:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); InitUI(); //Get the Button object Button btnConnect = (Button) findViewById(); (new () { public void onClick(View v) { //The event is handled here //ConnectServer(); //UDPClient udpET = new UDPClient("192.168.0.14","123"); //(); //Display Toast("Clicked\"Link\"button"); } }); //Send Button btnConnect = (Button) findViewById(); (new () { public void onClick(View v) { //The event is handled here SendText(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in . int id = (); if (id == .action_settings) { return true; } return (item); } /* Show Toast */ public void DisplayToast(String str) { (this, str, Toast.LENGTH_SHORT).show(); } public void InitUI() { TextView text=(TextView)findViewById(); ("192.168.0.14"); text = (TextView)findViewById(); ("Udp Client Send Test"); } // connect server public void SendText() { TextView editIP=(TextView)findViewById(); TextView editText=(TextView)findViewById(); String message = ().toString() + "\r\n"; UDPClient udpET = new UDPClient(().toString(), message); (); } }
:
package ; import ; import ; import ; import ; import ; import ; public class UDPClient extends Thread{ 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 = (m_serverIP); DatagramSocket socket = new DatagramSocket(); (TIMEOUT); DatagramPacket sendPacket = new DatagramPacket(bytesToSend,,serverAddress,servPort); (sendPacket); (); } catch (SocketException e){ (); }catch(IOException e){ (); } } }
3. activity_main.xml:
<LinearLayout xmlns:andro xmlns:tools="/tools" android: 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="" > <EditText android: android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/text_IP" /> <Button android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/text_connect" /> <EditText android: android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/text_filePath" > <requestFocus /> </EditText> <Button android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/text_sel" /> <Button android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/text_send" /> </LinearLayout>
For more information about Android related content, please check out the topic of this site:Android communication methods summary》、《Android debugging skills and solutions to common problems》、《Android development introduction and advanced tutorial》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.