In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to use Handler and Message in Android. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Message:
Message is a message passed between threads, which can carry a small amount of information internally for exchanging data between different threads. In addition to the what field, you can use the arge1 and arg2 fields to carry some integer data, and the obj field to carry an Object object.
Handler
Handler, as its name implies, means processor, and it is mainly used to send and process messages. Messages are generally sent using the sendMessage () method of Handler, and the messages sent are eventually passed to the handleMessage () method of Handler after a series of twists and turns.
MainActivity:
Import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener {/ / defines the integer UPDATE_TEXT as bright, which is used to indicate that the action of updating TextView is public static final int UPDATE_TEXT = 1; private TextView text; private Button changeText / / create a Handler private Handler handler = new Handler () {public void handleMessage (Message msg) {switch (msg.what) {case UPDATE_TEXT: / / UI operation can be performed here / / A pair of msg.obj can be String cast String string= (String) msg.obj; text.setText (string); break; default: break }}; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); text = (TextView) findViewById (R.id.text); changeText = (Button) findViewById (R.id.change_text); changeText.setOnClickListener (this) @ Override public void onClick (View v) {switch (v.getId ()) {case R.id.change_text: new Thread (new Runnable () {@ Override public void run () {/ / create a message / / set the value of the what field to UPDATE_TEXT Mainly to distinguish between different message / / set the contents of message.obj / / call the message object of Handler / / the handlermessage object in handler is String string= "Nice to meet you" running in the main thread Message message = new Message (); message.what = UPDATE_TEXT; message.obj=string; handler.sendMessage (message);}}) .start (); break; default: break;}}
Activity_main:
These are all the contents of the article "how to use Handler and Message in Android". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.