The creation and life cycle of non-binding android:service

Source: Internet
Author: User

If an Android service is created with a non-binding, it will no longer be able to contact it after it is created, i.e., message parameters cannot be passed;

So if you want to create and still have a connection, then you can refer to my previous blog "Android:service Binding and binding, service and activity communication", This refers to the binding of the service and the content related to activity communication (such as topic O (^▽^) o).


The non-binding of the service is very simple to create, similar to the start activity.
Just call StartService () to create it, and call Stopsercice () to stop the service.

Look at the life cycle diagram of the service:
1. The life cycle of the non-binding start on the left;
2. The right is the binding life cycle;


Here is an example, the interface is still very simple, only two buttons, one created, one stop, when pressed to trigger the corresponding event, while toast information.
In the service life cycle method, I use Logcat to output tags.

Run as follows:

Logcat is as follows:
1. When StartService () creates a service, call OnCreate () and Onstartcommand () two life cycle methods;
2. When the service is created, when you click the Create button, you will see that only Onstartcommand () is called and no OnCreate () is called.
3. When StopService () stops service, call the life cycle method of OnDestroy ();
4. You will find that Onbind () and Unonbind () have not been called, which is why? Because this is a non-binding start ...

Okay, finally, the code is attached:

Mainactivity.java:

 PackageActivity.wyc.com.service;ImportAndroid.content.Intent;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public  class mainactivity extends actionbaractivity {    PrivateButton Btnobj,btn02obj;PrivateIntent Intent;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Btnobj = (Button) Findviewbyid (r.id.btnobj);        Btn02obj = (Button) Findviewbyid (r.id.btn02obj); Intent =NewIntent (mainactivity. This, Myservice.class); Btnobj.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v)                {StartService (intent); Toast.maketext (mainactivity. This,"StartService", Toast.length_short). Show ();        }        }); Btn02obj.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v)                {StopService (intent); Toast.maketext (mainactivity. This,"StopService", Toast.length_long). Show ();    }        }); }}

Myservice.java:

 PackageActivity.wyc.com.service;ImportAndroid.app.Service;ImportAndroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.util.Log; Public  class myservice extends Service {    PrivateString MyTag ="MyTag"; Public MyService() {    }@Override     PublicIBinderOnbind(Intent Intent) {LOG.I (MyTag,"Onbind");//Todo:return The communication channel to the service.        Throw NewUnsupportedoperationexception ("not yet implemented"); }@Override     Public void onCreate() {Super. OnCreate (); LOG.I (MyTag,"OnCreate"); }@Override     Public int Onstartcommand(Intent Intent,intFlagsintStartid) {log.i (MyTag,"Onstartcommand");return Super. Onstartcommand (Intent, flags, Startid); }@Override     Public void OnDestroy() {log.i (MyTag,"OnDestroy");Super. OnDestroy (); }@Override     Public Boolean Onunbind(Intent Intent) {LOG.I (MyTag,"Onunbind");return Super. Onunbind (Intent); }}

Activity_main.xml:

<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:paddingleft="@dimen/activity_horizontal_margin"    Android:paddingright="@dimen/activity_horizontal_margin"    Android:paddingtop="@dimen/activity_vertical_margin"    Android:paddingbottom="@dimen/activity_vertical_margin"    android:orientation="Vertical"    Tools:context=". Mainactivity ">    <buttonandroid:text="Start service"android:textsize="20sp"  Android:layout_width="Match_parent"android:layout_height="Wrap_content" android:id="@+id/btnobj" />                                            <buttonandroid:text="Stop service"android:textsize="20sp"  Android:layout_width="Match_parent"android:layout_height="Wrap_content"  Android:id="@+id/btn02obj" />                                        </linearlayout>

Androidmanifest.xml:
"Note" Service is also the same as the activity of the components, one of the four core components, so also must be registered in the Androidmanifest.xml;

<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android"  package ="Activity.wyc.com.service" >        <applicationandroid:allowbackup="true"android:icon="@ Drawable/ic_launcher "android:label=" @string/app_name "android:theme= "@style/apptheme" >                                        <activityandroid:name=". Mainactivity "android:label=" @string/app_name " >                                    <intent-filter>                <action android:name="Android.intent.action.MAIN" />                <category android:name="Android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <serviceandroid:name=". MyService "android:enabled=" true "android:exported=" true " >                                            </Service>    </Application></manifest>

The following is the source code (Baidu cloud disk, Android studio written):

Http://pan.baidu.com/s/1mg7kS8S

The creation and life cycle of non-binding 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.