Android Service (1)

Source: Internet
Author: User

In Android, services run in the background, with the same level as activity. Since the service is a service running in the background, it is invisible and has no interface. You can start a service to play music, record changes in your location, or start a service to run and listen for a certain action.

Like other components, a service runs in the main thread, so it cannot be used for time-consuming requests or actions. You can open a thread in the Service to perform time-consuming actions in the thread.

So how to use the service?

Old Rules: Let's start with basic knowledge.

I. Basic Knowledge

There are two types of services:

1: Local Service,Local Service for ApplicationsProgramInternal. In service, you can call context. startservice () to start and call context. stopservice () to end. You can call service. stopself () or service. stopselfresult () internally to stop the service. No matter how many times startservice () is called, you only need to call stopservice () once to stop.

2: remote serviceRemote service is used between applications in the Android system. You can define an interface and expose it for other applications to perform operations. The client establishes a connection to the service object and calls the service through that connection. Call the context. bindservice () method to establish a connection and start it to call context. unbindservice () to close the connection. Multiple clients can be bound to the same service. If the service is not loaded yet, bindservice () loads it first.
It can be provided for reuse by other applications, such as defining a weather forecast service and providing a call with other applications.

Let's first look at the service lifecycle:


Context. startservice ()-> Oncreate ()-> onstart ()-> Service Running-- Call context. stopservice ()-> ondestroy ()

Context . Bindservice ()-> Oncreate ()-> onbind () -> Service Running -- Call> onunbind ()-> Ondestroy ()
From the appeal, you can know that the service is started in different ways, corresponding to the local and remote respectively.

Ii. Practice

We can define a local service to inherit from the service, and then play the media player or record geographical location changes in this service. Generally, if our service needs to interact with the activity, we can define an internal class and return this service. Of course, we need to consider that if we start the service by binding, the internal class can be defined as inheriting the binder and then returning the local service.CodeAs follows.

View code

 Package Com. Dongzi;

Import Android. App. Service;
Import Android. content. intent;
Import Android. Media. mediaplayer;
Import Android. OS. Binder;
Import Android. OS. ibinder;
Import Android. util. log;

Public Class LocalService Extends Service {

Private Static Final String tag = "LocalService ";
Private Ibinder binder = New LocalService. localbinder ();

@ Override
Public Ibinder onbind (intent ){

Return Binder;
}
Mediaplayer = Null ;
@ Override
Public Void Oncreate (){
Log. I (TAG, "oncreate ");
// You can start the media player here.
// If (mediaplayer = NULL)
// Mediaplayer = mediaplayer. Create (this, Uri );
Super . Oncreate ();
}

@ Override
Public Void Onstart (intent, Int Startid ){
Log. I (TAG, "onstart ");
Super . Onstart (intent, startid );
}

@ Override
Public Int Onstartcommand (intent, Int Flags, Int Startid ){
Log. I (TAG, "onstartcommand ");
Return Start_sticky;
}



@ Override
Public Void Ondestroy (){
Log. I (TAG, "ondestroy ");
Super . Ondestroy ();
}


// Define content class inheritance Binder
Public Class Localbinder Extends Binder {
// Back to Local Service
LocalService getservice (){
Return LocalService. This ;
}
}


}

We can know from above

// Define content class inheritance Binder
Public class localbinder extends binder {
// Return to the local service
LocalService getservice (){
Return LocalService. This;
}
}

You can return this service, and then the activity can call the service method through the service.

So how to start the service? From the basic knowledge above, we know there are two methods:

View code

//Start the service
Private VoidStartcustomservice (){
Intent intent =NewIntent (This, LocalService.Class);
Startservice (intent );
}

2nd binding methods:

View code

LocalService = Null ;
// Start the service using bindservice
Private Void Binderservice (){
Intent intent = New Intent ( This , LocalService. Class );
Bindservice (intent, New Serviceconnection (){
@ Override
Public Void Onserviceconnected (componentname, ibinder binder ){
// When you call the bindservice method to start a service, if the service needs to interact with the activity,
// The onbind method is used to return the ibinder and the current local service.
LocalService = (LocalService. localbinder) binder). getservice ();
// You can prompt the user or call some methods of the service.
}

@ Override
Public Void Onservicedisconnected (componentname ){
LocalService = Null ;
// The user is prompted.
}
}, Context. bind_auto_create );
}

When binding a service, you need a service connection object, serviceconnection. Once the service is connected, the onserviceconnected method is called. In this method, we can return our local service object, the code is specific. when the service is disconnected, the onservicedisconnected method is called. We can clear some service resources.

Next, we will explain some aidl knowledge. These are closely related to services.

 

 

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.