欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android编程之多线程编程实例分析

程序员文章站 2022-06-23 10:15:15
本文实例讲述了android编程之多线程编程实现方法。分享给大家供大家参考。具体分析如下: 该功能与前面《》中一样,当按下键后,发送文本框中数据给pc。不同的是把发送数据...

本文实例讲述了android编程之多线程编程实现方法。分享给大家供大家参考。具体分析如下:

该功能与前面《》中一样,当按下键后,发送文本框中数据给pc。不同的是把发送数据的功能放在一个线程socket_send中。

一、环境:

win7 + eclipse + ndk

二、代码:

主类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 when the activity is first created. */ 
 @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
  //开辟控件空间 
  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; 
   } 
  }); 
 } 
}

线程类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"; //这是要传输的数据 
  byte out [] = str.getbytes(); //把传输内容分解成字节 
  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) 
   { 
    //获得输入框文本 
    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; 
   } 
  } 
 } 
}

注意:程序中采用的线程间通信的方法是把需要共享的变量设置成public属性

希望本文所述对大家的android程序设计有所帮助。