Service of Android four components

Source: Internet
Author: User

Service
Is the component that runs silently in the background, can be understood as the activity without foreground, suitable for running code that does not need the foreground interface
The service can be manually shut down and not restarted, but if it is shut down automatically, the memory will restart
StartService the life cycle of the start service
Oncreate-onstartcommand-ondestroy
Repeated calls to StartService cause Onstartcommand to be called repeatedly

Process priority
1. Foreground process: has foreground activity (Onresume method is called)
2. Visible process: Has visible activity (OnPause method is called)
3. Service process: Not to be recycled as a last resort, and will be restarted even if it is recovered and memory is sufficient
4. Background process: Have background activity (the activity's OnStop method is called), it is easy to be recycled
5. Empty process: No activity is running and can easily be recycled

How to display the start service and shutdown service

1 Write Java class inherit service

2 registering the service in the manifest file

3 Intent = new Intent (this, myservice.class);

4 StartService (Intent);

5 StopService (Intent);

Two ways to start a service

Two ways to start a service
StartService: After the service is started, it doesn't have a dime relationship with the component that started it.
Bindservice: Follow the component that started it die
Life cycle methods for binding services and unbinding services: Oncreate->onbind->onunbind->ondestroy

Bindservice can do StartService:

  We want to invoke the method in the activity in the service, cannot call through StartService, the service object can only be created by the system, this time we need to invoke the method in the service to implement through the binding service

Idea: First define a method in the service define an inner class, this inner class inherits the binder, we define a method in this inner class, and in this method we call the method that we want to call in the service, define an interface, define a method in the interface, The methods in the inner class implement this method by implementing this interface. In the Onbind method of the service, returns the inner class of the definition.

Start the binding service in activity, Bindservice (intent, Conn, bind_auto_create); When the service is started The Conn class implements the method in the Serviceconnection interface onserviceconnected (componentname name, IBinder service) parameter IBinder Service is the inner class object that we return in the Onbind method in the service method. The method in the service is invoked by returning the inner class object.

Analogy: The service is likened to leadership, leadership has methods, we want to find the leadership of the card, but also need to call the method of leadership, but we can not directly find leadership, only through the leadership of the intermediary object, through him to call the leadership of the way, so as to achieve the purpose of certification.

The code shows:

public class Leaderservice extends Service {@Overridepublic ibinder onbind (Intent Intent) {//Returns a Binder object, This object is the Middleman object return new Zhoumi ();} Class Zhoumi extends Binder implements Publicbusiness{public void Qianxian () {Banzheng ();}} public void Banzheng () {System.out.println ("certificate");}}

Registration Services

<service android:name= "Com.xiaochen.banzheng.LeaderService" ></service>

//

public class Mainactivity extends Activity {        private Intent intent;private Myserviceconn Conn; Publicbusiness PB; @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate ( Savedinstancestate);        Setcontentview (R.layout.activity_main);//service put into intent object        Intent = new Intent (this, leaderservice.class);        conn = new Myserviceconn ();        Binding leader Service        Bindservice (Intent, Conn, bind_auto_create);    }        public void Click (View v) {    //Invoke service's Certificate method    PB. Qianxian ();    }    Class Myserviceconn implements serviceconnection{    //Connection service succeeded, this method calls @overridepublic void onserviceconnected ( ComponentName name, IBinder service) {//TODO auto-generated method STUBPB = (publicbusiness) service;} @Overridepublic void onservicedisconnected (componentname name) {//TODO auto-generated Method stub}}}    

  

Mixed use of two startup methods

  When we listen to music, music is a service process, startservice (intent); But we need to be in the foreground to call music in the activity, such as playing music, pause music. At this time, when using the service process, we can not access to the music service method, to use the music service method, can only use the binding process, but the binding process, while the activity is destroyed, also died, this time you could use two startup methods mixed use

Intent Intent = new Intent (this, musicservice.class);        Mixed call        //In order to turn the service process into a service process        startservice (intent);        In order to get        the Middleman object Bindservice (Intent, New Musicserviceconn (), bind_auto_create);

First start, then bind, destroy the first unbind, in Destory

 Registering a broadcast recipient with a service

Android four components are to be registered in the manifest file

Configure broadcast receivers with code
You can use the manifest file to register
Once the broadcast is issued, the system will go to all the manifest files, which broadcast receiver's action and broadcast action is matched, if found, the broadcast receiver's process to start up
You can register with code
When you need to use the broadcast receiver, execute the registered code, do not need to execute the de-registration code, some broadcasts do not need to always be accepted

Special broadcast Receivers
There are some broadcast receivers in Android that must be registered with the code, and the manifest file registration is invalid.
1. Screen lock screen and unlock
2. Power Change

Broadcast receivers are special and can be registered either in the manifest file or directly with the code
Some broadcast receivers must be registered with the code
Power change
Screen lock screen and unlock

Code representation, registering a broadcast recipient with a service

Service of Android four components

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.