Android中使用socket通信实现消息推送的方法详解

2019-12-10 18:13:05于海丽

2.SocketClient工程

Android,socket,消息推送

该工程是一个Android的工程,只有一个MainActivity.java和activity_main.xml文件,

先看一下activity_main.xml布局文件:

<LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
  xmlns:tools="http://www.easck.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  tools:context=".MainActivity" 
  android:orientation="vertical" > 
   
  <LinearLayout  
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <EditText  
      android:id="@+id/ip_edt" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:hint="ip" 
      android:text="172.16.1.200"/> 
    <EditText  
      android:id="@+id/port_edt" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:hint="port" 
      android:text="2000"/> 
  </LinearLayout> 
  <Button  
    android:id="@+id/start_btn" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="start"/> 
  <EditText  
    android:id="@+id/socket_id_edt" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="socketID"/> 
   
 
   
  <EditText  
    android:id="@+id/msg_edt" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minLines="5" 
    android:hint="content" 
    android:gravity="top" 
    /> 
  <Button  
    android:id="@+id/send_btn" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="send"/> 
  <TextView  
    android:id="@+id/console_txt" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"/>   
  <Button  
    android:id="@+id/clear_btn" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="clear"/> 
</LinearLayout>