最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程實(shí)現(xiàn)簡(jiǎn)單的UDP Client實(shí)例

 更新時(shí)間:2016年04月08日 11:47:07   作者:bluebbc  
這篇文章主要介紹了Android編程實(shí)現(xiàn)簡(jiǎn)單的UDP Client,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)UDP Clinet客戶(hù)端的實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程實(shí)現(xiàn)簡(jiǎn)單的UDP Client。分享給大家供大家參考,具體如下:

該代碼在4.2.2內(nèi)調(diào)試通過(guò)

1.記得加權(quán)限

<uses-permission android:name="android.permission.INTERNET"/>

注意:Android 4.0之后,就不能在主線程進(jìn)行socket通信,否則會(huì)拋異常。

2.代碼

MainActivity.java:

package mao.example.quicksend;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    InitUI();
    //獲得Button對(duì)象
    Button btnConnect = (Button) findViewById(R.id.buttonConnect);
    btnConnect.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v)
      {
        //這里處理事件
        //ConnectServer();
        //UDPClient udpET = new UDPClient("192.168.0.14","123");
        //udpET.start();
        //DisplayToast("點(diǎn)擊了\"連接\"按鈕");
      }
    });
    //發(fā)送Button
    btnConnect = (Button) findViewById(R.id.buttonSend);
    btnConnect.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v)
      {
        //這里處理事件
        SendText();
      }
    });
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, 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 AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
  /* 顯示Toast */
  public void DisplayToast(String str)
  {
    Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
  }
  public void InitUI()
  {
    TextView text=(TextView)findViewById(R.id.editTextIP);
    text.setText("192.168.0.14");
    text = (TextView)findViewById(R.id.editTextFilePath);
    text.setText("Udp Client Send Test");
  }
  // connect server
  public void SendText() 
  {
    TextView editIP=(TextView)findViewById(R.id.editTextIP);
    TextView editText=(TextView)findViewById(R.id.editTextFilePath);
    String message = editText.getText().toString() + "\r\n"; 
    UDPClient udpET = new UDPClient(editIP.getText().toString(), message);
    udpET.start();
  }
}

UDPClient.java:

package mao.example.quicksend;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.lang.String;
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 = InetAddress.getByName(m_serverIP);
      DatagramSocket socket = new DatagramSocket();
      socket.setSoTimeout(TIMEOUT);
      DatagramPacket sendPacket = new DatagramPacket(bytesToSend,bytesToSend.length,serverAddress,servPort);
      socket.send(sendPacket);
      socket.close();
    } catch (SocketException e){
      e.printStackTrace();
    }catch(IOException e){
      e.printStackTrace();
    }
  }
}

3. activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/LinearLayout1"
  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="mao.example.quicksend.MainActivity" >
  <EditText
    android:id="@+id/editTextIP"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/text_IP" />
  <Button
    android:id="@+id/buttonConnect"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/text_connect" />
  <EditText
    android:id="@+id/editTextFilePath"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/text_filePath" >
    <requestFocus />
  </EditText>
  <Button
    android:id="@+id/buttonFileSel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/text_sel" />
  <Button
    android:id="@+id/buttonSend"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/text_send" />
</LinearLayout>

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android通信方式總結(jié)》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android Scroller的使用方法

    Android Scroller的使用方法

    這篇文章主要為大家詳細(xì)介紹了Android Scroller的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • 10種提升android運(yùn)行效率的建議

    10種提升android運(yùn)行效率的建議

    這篇文章主要分享提升android運(yùn)行效率的建議,如何讓安卓程序在有限的內(nèi)存和電池資源下流暢快速有效率的運(yùn)行,下面給大家分享下面給出了10種實(shí)踐中的建議,需要的朋友可以參考一下
    2021-11-11
  • Android Studio報(bào):“Attribute application@theme or @ icon ”問(wèn)題的解決

    Android Studio報(bào):“Attribute application@theme or @ icon ”問(wèn)題的解

    這篇文章主要給大家介紹了關(guān)于Android Studio報(bào):“Attribute application@theme or @ icon ”問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • Android自定義DataGridView數(shù)據(jù)表格控件

    Android自定義DataGridView數(shù)據(jù)表格控件

    這篇文章主要介紹了Android自定義DataGridView數(shù)據(jù)表格控件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android設(shè)置鈴聲實(shí)現(xiàn)代碼

    Android設(shè)置鈴聲實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android設(shè)置鈴聲實(shí)現(xiàn)代碼,以實(shí)例形式分析了Android中鈴聲設(shè)置的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-10-10
  • 解決Android WebView攔截url,視頻播放加載失敗的問(wèn)題

    解決Android WebView攔截url,視頻播放加載失敗的問(wèn)題

    這篇文章主要介紹了解決Android WebView攔截url,視頻播放加載失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Android SharedPreference存儲(chǔ)文件三步走

    Android SharedPreference存儲(chǔ)文件三步走

    SharedPreferences是安卓平臺(tái)上一個(gè)輕量級(jí)的存儲(chǔ)類(lèi),用來(lái)保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時(shí),將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時(shí),再?gòu)腟haredPreferences中將值取出
    2023-01-01
  • Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法

    Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法

    這篇文章主要介紹了Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-06-06
  • Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法

    Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法

    這篇文章主要介紹了Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法,實(shí)例分析了Android中ListView控件的使用技巧,需要的朋友可以參考下
    2016-01-01
  • Flutter多項(xiàng)選擇彈窗實(shí)現(xiàn)詳解

    Flutter多項(xiàng)選擇彈窗實(shí)現(xiàn)詳解

    這篇文章介紹了Flutter多項(xiàng)選擇彈窗實(shí)現(xiàn)詳解,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2021-11-11

最新評(píng)論

凤城市| 泰顺县| 红桥区| 荥阳市| 岢岚县| 来凤县| 贺兰县| 新津县| 浦东新区| 丰城市| 抚顺市| 华宁县| 衡阳县| 鄢陵县| 出国| 策勒县| 玉林市| 慈溪市| 黎城县| 镇平县| 北碚区| 石林| 海丰县| 邯郸市| 枞阳县| 兴义市| 东城区| 沙河市| 买车| 昌黎县| 长阳| 西乌珠穆沁旗| 乐山市| 广安市| 疏附县| 永德县| 于田县| 九龙县| 贵定县| 紫金县| 宜春市|