Two ways to enable Android Services (service) and the lifecycle of services

Source: Internet
Author: User

How wirelessly services are opened

A: Start the way to start the service

Calling function: StartService (Intent)->oncreate ()->onstart ()/onstartcommand ()->ondestroy ()
Features: The service will not be opened repeatedly, will only call OnStart (), the service is only stopped once.
Second: The use of bind way to develop services
Calling function: Bindservice (Intent ...) ->oncreate ()->onbind ()->onunbind ()->ondestroy ();
Feature: Bindings do not call OnStart () [obsolete] and Onstartcommand () methods.

The difference between the two types of services:
Start Mode Development Service, once the service is opened and the caller has no relationship whatsoever. For example, our service is invoked in activity, and when activity is closed, the service does not close.
Bind mode to open the service, the caller is gone. Services are also closed and can be understood as die.

It is easier to start the service in the same way. Focus on the way bind is.

Examples:
1. Set three buttons inside the layout

2. Set the Listener event for the button. There are several ways of doing it.
3. Handle the event.

When you click Bind:

    /*绑定的创建方式    */    new Intent(this, MyService.class);    bindService(intent, conn, BIND_AUTO_CREATE);

Conn is its own implementation of the functional class, inherited from the serviceconnection.
Complete code such as the following:

 Public  class mainactivity extends actionbaractivity {    PrivateMyConn Conn;PrivateCall C;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//call The function of a similar middleman. } Public void Bind(View v) {conn =NewMyConn (); Intent Intent =NewIntent ( This, Myservice.class);    Bindservice (Intent, Conn, bind_auto_create); } Public void Unbind(View v)        {Unbindservice (conn); c =NULL; }/ * * Invoke the method in the service */     Public void Pager(View v)    {C.callmethodinservice (); }Private  class myconn implements serviceconnection{        @Override         Public void onserviceconnected(componentname name, IBinder service) {System.out.println ("Invoke the method inside the service");        c = (call) service; }@Override         Public void onservicedisconnected(componentname name) {        }    }}

Service codes such as the following:

 Public  class myservice extends Service {    @Override     PublicIBinderOnbind(Intent Intent) {System.out.println ("The service was successfully bound.");return NewCall (); }@Override     Public void onCreate() {System.out.println ("OnCreate");Super. OnCreate (); }@Override     Public int Onstartcommand(Intent Intent,intFlagsintStartid) {System.out.println ("Onstartcommand");return Super. Onstartcommand (Intent, flags, Startid); }@Override     Public void OnDestroy() {System.out.println ("OnDestroy");Super. OnDestroy (); } Public void Methodinservice() {Toast.maketext ( This,"Methodinservice",0). Show (); } Public  class  call extends Binder{         Public void Callmethodinservice() {methodinservice (); }    }}

Layout file:

<linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  xmlns:tools  =" Http://schemas.android.com/tools " android:layout_width  = "match_parent"  android:layout_height  =" match_parent " android:orientation  =" vertical " tools:context  = " Com.example.servicelife.MainActivity ";     <button android:onclick="bind"android:layout_width="Fill_parent"  android:layout_height="Wrap_content"android:text="Start"/>                            <button android:onclick= "unbind"android:layout_width=" Fill_parent "android:layout_height=" Wrap_content "android:text=" End " />                            <button android:onclick="Call"android:layout_width="Fill_parent"  android:layout_height="Wrap_content"android:text="method called in service"/>                        </linearlayout>

Click to download the source code

Two ways to enable Android Services (service) and the lifecycle of services

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.