1. Implementing multithreading
1. Create threads
(1) Create threads through Thread
Thread thread = new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub } });
(2) Create threads by implementing the Runnable() interface
public class MainActivity extends Activity implements Runnable{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); @Override public void run() { // TODO Auto-generated method stub } }
2. Thread Opening
thread.start();
3. Sleeping of threads
Thread.sleep(1000);//Dormancy for one second
4. Interrupt threads
public class MainActivity extends Activity implements Runnable{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LooperThread thread = new LooperThread(); thread.start(); thread.interrupt(); } @Override public void run() { // TODO Auto-generated method stub while(!Thread.currentThread().isInterrupted()){ // do something } } }
2. Handler Messaging Mechanism
The ready-made usage was introduced earlier, but the UI could not be modified in the newly built sub-threads. If you want to operate, you will report Only the original thread that created a view hierarchy can touch its views. To this end, Android introduces Handler messaging mechanism to realize the operation of UI interface in the thread.
1. Introduction to Looper
In Android, a thread corresponds to a Looper object and a Looper object corresponds to a MessageQueue. MessageQueue is used to store messages. In MesageQueue, stored messages are executed according to FIFO principles.
Looper objects are used to open a message loop for threads to manipulate MessageQueue. By default, the system automatically creates Looper objects for the main thread and opens the message loop. Therefore, there will be no error in creating Handler objects in the main thread with the following code, while errors will be reported in non-main threads.
Handler handler = new Handler();
If you want to create Handler objects in non-main threads, you must use Looper:
public class LooperThread extends Thread { public Handler h; @Override public void run() { // TODO Auto-generated method stub super.run(); Looper.prepare(); // h = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub Log.i("Looper", String.valueOf(msg.what)); } }; Message m = h.obtainMessage(); m.what = 0x11; h.sendMessage(m); Looper.loop(); } }
2. Introduction to Handler
Handler has two main functions:
(1) Send a message or Runnable to a MessageQueue using post() or sendMessage() methods, which can specify a delay time or bound Bundle data. When MessageQueue loops to the Message, the handlerMessage() method is called to process it.
(2) Communicate with the main thread in the sub-thread, that is, with the UI in the workthread.
3. Introduction to Message Class
Message has the following five properties
1.arg1 int is used to store integer data
2.arg2 int is used to store integer data
3.obj Object is used to store any object of type Object sent to the receiver
4.replyTo Messenger is used to specify where the Message should go
5.what int user-defined message code
In use, the following three points should be noted:
1. Usually use Message. get () or Handler.obtainMessage() to get empty message objects to save resources.
2. If a Message only needs to carry simple int information, it is preferable to use arg1 and arg2 to pass it, which saves more memory than Bundle.
3. Identify information with Message.what whenever possible