Sub-threads in Android activity and service (Entry level)

Source: Internet
Author: User

Sub-threads in Android activity and service (Entry level)

1. First, the activity in an android program is a thread, and the service and activity are also a thread.

2. Start a sub-thread in the activity. The current activity finishes destroy and drops the sub-thread.

3. the thread in the service is similar to the activity. The service is running even if the thread is stopped (stop the service first and then kill the recently used process to stop the thread, if you directly kill the process, android will automatically start the service again. At this time, even if you stop the service thread, it will continue to run unless it is shut down)

Enable a subthread in activity

Public class SecondActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_lout); System. out. println ("the current Thread executes ==== SecondActivity ====" + Thread. currentThread (). getId (); new Thread (new Runnable () {@ Overridepublic void run () {while (true) {System. out. println ("the current Thread executes = 22222 =" + Thread. currentThread (). getId (); try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();}}}}). start () ;}@ Overrideprotected void onDestroy () {System. out. println ("the current Thread has executed ==========" + Thread. currentThread (). getId (); super. onDestroy ();}}
Activity finish sub-threads can still run, even if the program exits the sub-thread is running (unless the recently used process is killed in the Task Manager)


Start a sub-thread in service

Public class TestService extends Service {@ Overridepublic int onStartCommand (Intent intent, int flags, int startId) {// TODO Auto-generated method stubSystem. out. println ("the current Thread executes === TestService0000 ===" + Thread. currentThread (). getId (); new Thread (new Runnable () {@ Overridepublic void run () {while (true) {System. out. println ("the current Thread executes = 22222 =" + Thread. currentThread (). getId (); try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();}}}}). start (); return super. onStartCommand (intent, flags, startId) ;}@ Overridepublic void onDestroy () {System. out. println ("the current Thread executes ==== TestService = end ====" + Thread. currentThread (). getId (); super. onDestroy () ;}@ Overridepublic IBinder onBind (Intent intent) {// TODO Auto-generated method stubreturn null ;}}

The above three items are tested in android 4.4.2 and have not been tried in other versions. If anything is wrong, please correct it!

Please note that the life cycle of the sub-thread should not be completed by the activity until the sub-thread finishes processing the returned results to the UI. It may also provide some ideas for time-consuming or long-running cases.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.