As we all know, service and activity are running in the UI thread, time-out data reads and network time-consuming operations must create new threads, and here are a few ways to execute a new thread in the service.
1. The traditional Java approach
We all know that the new thread in Java can inherit the thread class, or implement the Runnable interface. The essence is to implement the runnable run method, where the bottom layer should be the JVM recognizes that the Run method allocates and creates threads. The Run method simply prepares the resources for the new thread and calls the
The start () native method to start the thread.
1 Public InterfaceRunnable {2 3 /**4 * Starts executing the active part of the class ' Code. This method is5 * Called when a thread was started that had been created with a class which6 * Implements {@codeRunnable}.7 */8 Public voidrun ();9}
1 Public class Implements Runnable
So is the same in the service, we can use the thread class:
1 @Override2 Public intOnstartcommand (Intent Intent,intFlagsintStartid) {3L.E (TAG, "Onstartcommand");4 NewThread (NewRunnable () {5 @Override6 Public voidrun () {7 8 }9 }). Start ();Ten return Super. Onstartcommand (Intent, flags, startid); One}
The Runnable interface can also be implemented:
1Runnable runnable=NewRunnable () {2 @Override3 Public voidrun () {4L.E (TAG, "***********************");5 }6 };7 @Override8 Public intOnstartcommand (Intent Intent,intFlagsintStartid) {9 Runnable.run ();Ten return Super. Onstartcommand (Intent, flags, startid); One}
2. Using Intentservice
Specifically see the previous post http://www.cnblogs.com/hxy0107/p/4552486.html
3. Using Handlethread and Headler
Handler itself does not create a new thread, just a handle to the main UI thread and new thread communication, passing the message message. Detailed in the next Android process communication. Use the following methods:
1 FinalHandler myhandler=NewHandler () {2 3 @Override4 Public voidhandlemessage (Message msg) {5 if(msg.what==0x123){6L.E (TAG, "Handlemessage");7 }8 }9 };Ten @Override One Public voidOnStart (Intent Intent,intStartid) { A -L.E (TAG, "OnStart"); - the NewThread (NewRunnable () { - @Override - Public voidrun () { -Myhandler.sendemptymessage (0x123); + } - }). Start (); + Super. OnStart (Intent, startid); A}
The handler created in this way is created in the context of the context, so it is bound to the looper of the main thread of the UI, and message sequence messages are also Looper received by the UI. If we want handler to bind to a thread of our own definition, we must create the message sequence Looper on the new thread, Fortunately, Android has handlerthread.handlerthread itself is just a thread, but it internally implements its own message sequence looper, so that other threads can and our new thread through the handler to communicate ~ (handler must bind new thread's Looper)
1 New Handlerthread ("Myhandlerthread"); 2 Thread.Start (); 3 New Handler (Thread.getlooper ()); 4 mhandler.post (new Runnable () {...});
1 Myhandlerthread Myhandlerthread;2 @Override3 Public voidOnStart (Intent Intent,intStartid) {4 5L.E (TAG, "OnStart");6Myhandlerthread=NewMyhandlerthread ("Myhandlerthread");7 Myhandlerthread.start ();8 FinalHandler handlerelse=NewHandler (Myhandlerthread.getlooper ()) {9 Ten @Override One Public voidhandlemessage (Message msg) { A if(msg.what==0x111) { -L.E (TAG, "Handlerelse handlemessage"); - } the Super. Handlemessage (msg); - } - }; - + NewThread (NewRunnable () { - @Override + Public voidrun () { AHandlerelse.sendemptymessage (0x111); at } - }). Start (); - Super. OnStart (Intent, startid); -}
Two new threads are created above, where the handlerthread thread has looper and can implement actions like the main UI thread and other thread interactions.
4. Timed Send message can use Timertask,timertast is also implemented Runnable interface method, new thread timed to send messages.
1 New Timer (). Schedule (new timertask () {2 @Override3public void run () {4 myhandler.sendemptymessage (0x123); 5 }6 },0,2000);
5. Thread pool ThreadPool and Threadpoolexecutor
After eating the meal is writing ...
Several ways to execute a new thread in Android-service