本文实例讲述了android编程之多线程编程实现方法。分享给大家供大家参考。具体分析如下:
该功能与前面《android开发socket编程之udp发送实例分析》中一样,当按下键后,发送文本框中数据给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;
@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程序设计有所帮助。
您可能感兴趣的文章:Android 中 EventBus 的使用之多线程事件处理Android中的Handler与多线程应用实例Android SQLite3多线程操作问题研究总结Android中创建多线程管理器实例android 多线程技术应用Android编程之canvas绘制各种图形(点,直线,弧,圆,椭圆,文字,矩形,多边形,曲线,圆角矩形)Android编程根据系列图片绘制动画实例总结使用Android自定义控件实现滑动解锁九宫格android 自定义控件 自定义属性详细介绍Android开发之多线程中实现利用自定义控件绘制小球并完成小球自动下落功能实例