Seniors Internship Preparation 6_android Service

Source: Internet
Author: User

2015-5-9

1. What is the service?

2.Android multithreaded Programming

3. Basic usage of the service

3.1 Defining services

1) Create the MyService class to inherit the service class,

2) Implement the Onbind () method (for the binding of activities and services), overriding OnCreate () (called when Service is created), Onstratcommand (called at service startup), OnDestroy () method (called when the service is destroyed).

3) Each service (in fact, four components) must be registered in the Androidmanifest.xml to take effect

3.2 Starting and stopping services

With intent.

Start:

1 New Intent (This, MyService.  Class); 2 StartService (startintent); // Start the service

After the first execution of StartService (startintent), the OnCreate () method, Onstratcommand () method in MyService executes sequentially.

And then execute StartService (startintent) after execution StopService ()? Or do StartService () directly again, only the Onstratcommand () method executes.

Because the service has already been created, the OnCreate () method is not executed.

Stop it:

1.

1 New Intent (This, MyService.  Class); 2 StopService (startintent); // Stop Service

The OnDestroy () method executes.

2.

Alternatively, the service can stop itself, that is, calling the Stopself () method at a location in the MyService class can also stop the service.

3.3 Activities and services for communication

Take advantage of the Onbind () method in the Myserviece class.

//...

4. Life cycle of the service

1.startService ()----StopService ()/stopself ()

2.bindService ()----Unbindservice ()

3.startService () && bindservice ()----StopService () && unbindservice () (?: or direct stopself (), not tested)

5. More Tips for service

5.1 Using the front desk service

The main difference between front desk service and General Service (i.e. back office service) is that two points

1) When the system is out of memory, the backend service may be reclaimed and the foreground service will not be recycled.

2) Background service is silently running, can not see. And the front desk service will always have a running icon in the System status bar display, drop-down can see the details, similar to the effect of notification. For example, ink weather is the use of this point.

Usage: Use the notification +startforeground () method in the OnCreate () method of MyService ().

The specific code to check the book it.

5.2 Using Intentservice

The code in the service is run by default in the main thread, so the ANR (application not responding) situation can be easily seen if some time-consuming operations are performed directly in the service.

Processing method One is, multi-threaded + after completion of automatic stop

1  Public classMyServiceextendsservice{2 @override3      Publicibinder onbind (Intent Intent) {4         return NULL;5     }6     7 @override8      Public intOnstartcommand (Intent Intent,intFlagsintStartid) {9         NewThread (NewRunnable () {Ten @override One              Public voidrun () { A                 //dealing with specific logic - stopself (); -             } the }). Start (); -         return Super. Onstartcommand (Intent,flags,startid); -     } -}

Processing method Two is, do not consider multithreading and the completion of the automatic stop after processing, the Intentservice class on its own implementation of these two functions.

1  Public classMyintentserviceextendsintentservice{2     3      PublicMyintentservice () {4         Super("Myintentservice");//call the parameter constructor of the parent class5     }6     7 @override8     protected voidonhandleintent (Intent Intent) {9         //dealing with specific logicTen         //Prints the ID of the current thread OneLOG.D ("Myintentservice", "Thread ID is" + thread.currentthread (). GetId ());//can be found and the thread ID of the main thread is not too A     } -      - @override the      Public voidOnDestroy () { -         Super. OnDestroy (); -LOG.D ("Myintentservice", "OnDestroy executed");//This method can be found to execute automatically even if the StopService () or Stopself () method is not called.  -     } +}

6. Best Practices for services-----Scheduled tasks performed in the background

Code

Seniors Internship Preparation 6_android Service

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.