当前位置:网站首页>Android Development: the client obtains the latest value in the database in real time and displays it on the interface
Android Development: the client obtains the latest value in the database in real time and displays it on the interface
2022-04-23 18:56:00 【Mug mop fan】
Catalog
summary
I'm writing a program recently , The requirement is that the server-side program will constantly refresh the value of a field in the database , Then you need to write a client program to continuously read the field and display it on the interface . Here the database uses MySQL,Android Data interaction between client and server Volley frame .
Preparation
Volley yes Google Officially launched HTTP Method library , Use Volley The framework needs to be Android Studio Introduce the corresponding package , For details, please refer to my previous articles :
Android Use Volley Frame for data transmission
Main code
The whole implementation logic is that the user clicks “START” after , Start to continuously pull the value of the corresponding field in the database , Also on UI Control . meanwhile , Set flag bit getAgain, Its meaning is as follows :
getAgain = true: The current read operation has completed , Make the next read ;
getAgain = false:Volley The child thread is reading the value of the corresponding field in the database , The read operation is blocked .
The code is as follows :
1. Variable bit declaration :
public static boolean getAgain = true;
2.“START” Button Click event :
mBtnBegin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Start a loop refresh
new Thread(new Runnable() {
@Override
public void run() {
// Conduct 1000 Read operations
for(int i = 0; i < 1000; ++i){
// Read the flag bit to judge
if(getAgain){
// Enter the read state
getAgain = false;
// Pull the latest field value from the database
ShowMyCount(getApplicationContext(), LoginActivity.Username);
while (!getAgain){
// Data reading , To block
}
}
}
}
}).start();
}
});
3.“ShowMyCount()” Method :
Use Volley The framework interacts with the server. You can see the above article , For the functions to be realized in this paper , stay “ The query is successful ” Two statements need to be added to the statement block of :
// The query is successful , to update UI Interface
mTvCount.setText(count);
// The data reading is completed , Unblock state
getAgain = true;
Optimize
1. Updated in this article UI The interface is directly in “ShowMyCount()” Method used in setText() Method , but Android In development ,UI The update of the interface usually uses Handler Mechanism .
2. In this paper, the for Cycle... Cycle 1000 Times to roughly realize automatic cyclic reading , But more than 1000 It will not be updated after UI Interface , There are two solutions : Increase the number of cycles or use while loop .
——————————————————————————
Finally, post my official account. : WeChat search “ Tea migration ” Or scan the figure below . Usually update some programming related articles , Welcome to pay attention ~
版权声明
本文为[Mug mop fan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210603049963.html
边栏推荐
- Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
- 22 year flying Book manpower Kit
- 2022.04.23(LC_763_划分字母区间)
- Sogou cell thesaurus analysis (only extract words and word frequency)
- 7、 DOM (Part 2) - chapter after class exercises and answers
- 简化路径(力扣71)
- Use of kotlin collaboration in the project
- Is it safe to open an account in Bohai futures.
- 中金财富怎么样?在上边开户安全吗
- 2022.04.23(LC_714_买卖股票的最佳时机含手续费)
猜你喜欢
ctfshow-web362(SSTI)
STM32: LCD display
[today in history] April 23: the first video uploaded on YouTube; Netease cloud music officially launched; The inventor of digital audio player was born
2022.04.23(LC_763_划分字母区间)
七、DOM(下) - 章节课后练习题及答案
Tangle
Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
ESP32 LVGL8. 1 - textarea text area (textarea 26)
mysql_linux版本的下载及安装详解
os_authent_prefix
随机推荐
SSDB基础3
WebView saves the last browsing location
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
MySQL Téléchargement et installation de la version Linux
K210 serial communication
SSDB基础
Scrollto and scrollby
7、 DOM (Part 2) - chapter after class exercises and answers
Click the input box to pop up the keyboard layout and move up
Deeply understand what new and make in golang are and what are the differences?
Get a list of recent apps
C: generic reflection
12 examples to consolidate promise Foundation
Go language GUI framework Fyne Chinese garbled or not displayed
Using bafayun to control the computer
c1000k TCP 连接上限测试
From technical system to business insight, the closing chapter of the practice of small and medium-sized R & D team structure
#yyds干货盘点#stringprep --- 因特网字符串预备
Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
Golang 语言实现TCP UDP通信