场景:服务器不定时(间隔几秒)发送数据到app,app根据接收到的数据在webview上显示与之唯一对应的gif动图。
不能在主线程以外操作UI,android中把耗时的操作都放到子线程里面,主线程又叫UI线程。
直接代码!
1、文件所在位置
2、主界面xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/t1" android:layout_width="293dp" android:layout_height="64dp" android:gravity="center" android:text="显示结果" android:textSize="40dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.143" /> <WebView android:id="@+id/webView" android:layout_width="366dp" android:layout_height="327dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.492" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.47" /> <Button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.544" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.875" /> </androidx.constraintlayout.widget.ConstraintLayout>
3、主函数java
package com.example.mhandle; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.webkit.WebView; import android.widget.Button; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; public class MainActivity extends AppCompatActivity { private Handler handler; private TextView textView; private Button b1; private WebView webView; private String gifFilePath = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.t1); b1 = findViewById(R.id.b1); webView = findViewById(R.id.webView); handler = new MyHandler(); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new MyThread().start(); } }); } class MyHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); String str = (String) msg.obj; // Log.d("TTTT", "接收到的消息是" + str); switch (str){ case "4": gifFilePath = "file:///android_asset/gif/打篮球.gif";//首先将一张gif格式的动图放置在assets中 break; case "1": gifFilePath = "file:///android_asset/gif/跑步.gif";//首先将一张gif格式的动图放置在assets中 break; case "2": gifFilePath = "file:///android_asset/gif/开车.gif";//首先将一张gif格式的动图放置在assets中 break; default: gifFilePath = "file:///android_asset/gif/1.gif";//首先将一张gif格式的动图放置在assets中 } webView.loadUrl(gifFilePath); } } class MyThread extends Thread{ @Override public void run() { super.run(); InputStreamReader isr = null; BufferedReader br; // 要连接的服务端IP地址和端口 String host = "192.168.137.1"; int port = 9999; String order = "1" ; String message = "22"; while (true){ Socket socket = null; try { socket = new Socket(host, port); } catch (IOException e) { e.printStackTrace(); } try { Thread.sleep(1000*1); } catch (InterruptedException e) { e.printStackTrace(); } OutputStream outputStream = null; try { outputStream = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } PrintWriter pWriter = new PrintWriter(outputStream); pWriter.write(order+message); pWriter.flush(); try { socket.shutdownOutput(); } catch (IOException e) { e.printStackTrace(); } // 获得输入流 try { isr = new InputStreamReader(socket.getInputStream()); } catch (IOException e) { e.printStackTrace(); } br = new BufferedReader(isr); // todo 尝试 handler发送数据 Message mesg = handler.obtainMessage(); try { mesg.obj = br.readLine(); } catch (IOException e) { e.printStackTrace(); } handler.sendMessage(mesg); }}; } }