首页 > 解决方案 > How to display a message on android screen first and only then execute a method?

问题描述

I a developing an app which calculates log and Antilog of entered number and displays the same on the screen. However, as a part of the project this application also maliciously disconnect the connected Wifi network of the device. I have used wifiManager.disableNetwork(netId) and wifiManager.disconnect() to disconnect the connected network and used the infinite while loop to continue executing these.

However, I am not able to print the answer on the screen even when the display method is called first to display the answer and then the malicious method is called which repeatedly disconnects the connected Wifi network.

Is there any way by which, I can achieve the following:

  1. Calculate the log/Antilog
  2. Display the answer on the screen
  3. Call the method to execute the loop containing the code to disconnect the network

Note: I have 3 methods, 1 to calculate, 1 to display and 1 to run malicious code. However, the answer is displayed on the screen only after the method with malicious code completes executing the loop, if it is made as infinite loop the answer is never displayed and the application crashes. I want to display the answer first and only then execute the loop.

I am using JAVA on android studio. Any help is appreciated.

标签: javaandroid

解决方案


看来您在主线程(又名 UI 线程)上执行了无限循环。因此,该应用程序不会响应任何内容,因为它正忙于断开 wifi 连接。

我建议,你看看线程并在一个新线程中执行循环。

在循环中,您应该在每次迭代时检查它是否仍然应该循环中断它不应该的循环。


推荐阅读