How wirelessly services are opened
One: Start the service by starting the way
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-style development 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 open and the caller has no relationship, such as our service is invoked in the activity, when the activity is closed, the service will not be closed.
Bind mode to open the service, the caller is gone, the service will be closed, can be understood as die.
The way to start the service is relatively simple, focus on the way bind!
Example:
1. Set three buttons inside the layout
2. There are several ways to set the listener event for a button.
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.
The complete code is as follows:
Public class mainactivity extends actionbaractivity { PrivateMyConn Conn;PrivateCall C;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//call functions like a 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) { } }}
The service code is as follows:
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>
Two ways to enable Android Services (service) and the lifecycle of services