Multi-Threading Programming example analysis of Android programming _android

Source: Internet
Author: User
Tags thread class

This article is an example of how to implement a multithreaded programming approach to Android programming. Share to everyone for your reference. The specific analysis is as follows:

This feature is the same as the previous "UDP Send instance Analysis" of the Android development socket programming, when the key is pressed, send the data in the text box to the PC. The difference is to put the function of sending data in a thread socket_send.

First, the environment:

Win7 + eclipse + NDK

Second, code:

Main class Test_socket.java

Package test.soket; Import Com.test_button. 
R 
Import Java.io.DataOutputStream; 
Import java.io.IOException; 
Import Java.net.DatagramPacket; 
Import Java.net.DatagramSocket; 
Import java.net.InetAddress; 
Import Java.net.Socket; 
Import java.net.SocketException; 
Import java.net.UnknownHostException; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.widget.Button; 
Import Android.widget.TextView; 
 public class Test_socket extends activity {public static TextView show; 
 public static Button Press; 
 public static Boolean flag; /** called the activity is a. 
  * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.main); 
  Open control Space show = (TextView) Findviewbyid (R.ID.EDITTEXT1); 
  Press = (Button) Findviewbyid (R.id.button1); 
  Flag = false; 
  Soket_send thread = new Soket_send (); 
  Thread.init (); 
  Thread.Start (); Press.setonclIcklistener (New Button.onclicklistener () {@Override public void OnClick (View v) {flag = true; 
 } 
  }); } 
}

Thread class Socke_sendt.java:

Package test.soket; 
Import Java.io.DataOutputStream; 
Import java.io.IOException; 
Import Java.net.DatagramPacket; 
Import Java.net.DatagramSocket; 
Import java.net.InetAddress; 
Import Java.net.Socket; 
Import java.net.SocketException; 
Import java.net.UnknownHostException; 
Import java.util.LinkedList; 
Import Android.media.AudioFormat; 
Import Android.media.AudioRecord; 
Import Android.media.MediaRecorder;  
Import Android.util.Log; 
 public class Soket_send extends Thread {private static final int max_data_packet_length = 40; 
 Private byte[] buffer = new Byte[max_data_packet_length]; 
 Private Datagrampacket Datapacket; 
 Private Datagramsocket Udpsocket; 
  public void Init () {try {udpsocket = new Datagramsocket (5554); 
  catch (SocketException e) {//TODO auto-generated catch block E.printstacktrace (); 
  } datapacket = new Datagrampacket (buffer, max_data_packet_length); String str = "HELLO,JDH"; This is the data to be transferred byte out [] = Str.getbytes (); 
  The transmission content is decomposed into byte datapacket.setdata (out); 
  Datapacket.setlength (out.length); 
  Datapacket.setport (5554); 
   try {inetaddress broadcastaddr = inetaddress.getbyname ("192.168.0.248"); 
   Datapacket.setaddress (BROADCASTADDR); 
  Udpsocket.send (Datapacket); 
  catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } public void Run () {while (true) {if (Test_socket.flag) {//Get input box text charsequence str = 
    Test_socket.show.getText (); 
    byte out[] = Str.tostring (). GetBytes (); 
    Datapacket.setdata (out); 
    Datapacket.setlength (out.length); 
     try {inetaddress broadcastaddr = inetaddress.getbyname ("192.168.0.248"); 
     Datapacket.setaddress (BROADCASTADDR); 
    Udpsocket.send (Datapacket); 
    catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); 
   } Test_socket.flag = false; } 
  } 
 } 
}

Note: The method used to communicate between threads in a program is to set the variables that need to be shared into public properties

I hope this article will help you with your Android program.

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.