編輯:關於Android編程
本文實例講述了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; /** 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程序設計有所幫助。
一、概述隨著Android L版本的發布,RecyclerView已經逐漸地取代了ListView,用來顯示較多的數據集,RecyclerView相比ListView在性
一、Android Camera可以做哪些?1、功能拍攝相片 視頻錄制 取景器(掃描類應用,如人臉識別,名片識別,條形碼識別)2、根據Camera API實現自己的拍照程
在之前app寫完測試的時候,跑完整個老化階段包括數據收發都沒問題,鍵入 adb shell top -m 5 發現我的 app pid 占用的 CPU是最多的,其實我想說
TabWidget類似於Android 中查看電話薄的界面,通過多個標簽切換顯示不同內容。要實現這一效果,首先要了解TabHost,它是一個用來存放多個Tab標簽的容器。