- Straight
- First Kind
- The second Kind
- Third Kind
- Summarize
Straight
There are three scenarios for opening a service: If you are using the service directly, there is no need to bind, but if you want to use the methods inside the service, you bind.
-
The specific start-up situation is as follows:
-
① called
startService()
and then called
stopService()
.
-
② calls the
bindService()
method separately, and then
unbindService()
executes the internal method of the service.
The ③ is called first, then the method is called, and
-
startService()
bindService()
then
unbindService()
the last call
stopService()
.
-
Special cases:
The
-
methods in the service can be invoked whenever bindservice is used, regardless of whether or not to unbind and stop the service afterwards
The following are detailed descriptions of these three boot sequences, respectively.
Post the code before you explain it:
MyService class , it's not annotated.
Public class myservice extends Service { @Override PublicIBinderOnbind(Intent Intent) {return NewMybinder (); }@Override Public void onCreate() {System.out.println ("MyService onCreate (): Called by the system if the service is first created");Super. OnCreate (); }@Override Public Boolean Onunbind(Intent Intent) {System.out.println ("MyService onunbind (): Called when all clients has disconnected from a particular interface published by the service." );return Super. Onunbind (Intent); }@Override Public int Onstartcommand(Intent Intent,intFlagsintStartid) {System.out.println ("MyService Onstartcommand (): Called by the system every time a client explicitly starts the service by calling Android. Content. Context.startservice, providing the arguments it supplied and a unique integer token representing the start request. ");return Super. Onstartcommand (Intent, flags, Startid); }@Override Public void OnDestroy() {System.out.println ("MyService OnDestroy (): Called by the system to notify a Service that it's no longer used and is being removed.");Super. OnDestroy (); } Public void method1() {System.out.println ("MyService is Method1"); } Public void method2() {System.out.println ("MyService is Method2"); } class Mybinder extends Binder { Public void callMethod1() {method1 (); } Public void callMethod2() {method2 (); } }}
Mainactivity class
Public class mainactivity extends Activity { PrivateMybinder Mybinder;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); conn =NewMyserviceconnection (); } Serviceconnection Conn; Public void Start(View v) {Intent Service =NewIntent ( This, Myservice.class); StartService (service); } Public void Bind(View v) {Intent Service =NewIntent ( This, Myservice.class); Bindservice (SERVICE, Conn, context.bind_auto_create); } Public void Unbind(View v) {Unbindservice (conn); } Public void Stop(View v) {Intent Service =NewIntent ( This, Myservice.class); StopService (service); } Public void callmethod1(View v) {mybinder.callmethod1 (); }Private class myserviceconnection implements serviceconnection { @Override Public void onserviceconnected(componentname name, IBinder service) {System.out.println ("Myserviceconnection Connection Success"); Mybinder = (mybinder) service; }@Override Public void onservicedisconnected(componentname name) {System.out.println ("myserviceconnection disconnection Success"); } }}
activity_main.xml Page layout
<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 =; <button android:layout_ Width = "match_parent" android:layout_height< /span>= "wrap_content" android:onclick = "start" android:text = " StartService "/> <button android:layout_ Width = "match_parent" android:layout_height< /span>= "wrap_content" android:onclick = "bind" android:text = " Bindservice "/> <button android:layout_ Width = "match_parent" android:layout_height< /span>= "wrap_content" android:onclick = "unbind" android:text = " Unbindservice "/> <button android:layout_ Width = "match_parent" android:layout_height< /span>= "wrap_content" android:onclick = "stop" android:text = " StopService "/> <buttonandroid:layout_width="Match_parent"android:layout_height= "Wrap_content" Android:onclick="Callmethod1"android:text="Call Method1" /> </linearlayout>
First Kind
Called startService()
, and then called stopService()
. This applies to the direct use of the service and does not require external calls to the internal methods of the services.
In this one, we will click StartService and StopService respectively, in the class MyService, will onCreate()
start-represents the first time the service is created, to onDestory()
end-the service is destroyed on behalf of, one or more calls in the middle ( When repeatedly StartService) onStartCommand()
method-to indicate that the client wants to explicitly start the service.
When you click StartService, it triggers onCreate()
and onStartCommand()
methods, indicating that the service was created for the first time and is explicitly required by the client. After the two methods are executed, the service thread in the background starts.
Take a look at this picture:
This process corresponds to the log diagram and the application background service process diagram as follows:
It is clear to see that the call onCreate()
and the onStartCommand()
method, while the background of the service process has been started.
When the stopservice is clicked, it is triggered onDesctory()
and the background service process is destroyed.
Take a look at this picture:
This process corresponds to the log diagram and the application background service process diagram as follows:
It is clear to see that the method is invoked onDesctory()
, and that the background service thread is also destroyed.
The second Kind
The method is called separately to bindService()
bind the activity and service to achieve the purpose of the internal method of the service, and then call the Unbinding unbindService()
.
In this one, we will click Bindservice and Unbindservice respectively, in the class MyService, will onCreate()
start-represents the first time the service is created, to onDestory()
end-the service is destroyed on behalf of, in the middle, when the binding is successful, call onServiceConnected()
Indicates that activity and service connections are successful; When unbound, calls onUnbind()
indicate that activity and service are successfully disconnected.
When Bindservice is clicked, it triggers onCreate()
and onServiceConnected()
methods to achieve the purpose of invoking the internal method of the service. however , be aware that the background service process does not start
Take a look at this picture:
This process corresponds to the log diagram and the application background service process diagram as follows:
It can be clearly seen, called onCreate()
and onServiceConnected()
methods, but the backend service process does not start . After binding, you can invoke the internal method of the service, and that MyService is method1
is the proof.
When you click Unbindservice, the trigger onUnbind()
and onDestory()
method indicate the unbind and destroy service.
Take a look at this picture:
This process corresponds to the log diagram and the application background service process diagram as follows:
It is clear to see that calls onUnbind()
and onDesctory()
methods, and no service threads in the background. However, although the binding is unbound, we can still invoke the methods in the service.
Third Kind
Call First startService()
, then call the bindService()
method, then call unbindService()
, the last call stopService()
, this situation applies to the hope that the service can run in the background for a long time, as long as the StopService does not stop, you can also let the activity invoke the service method.
In the case of four combinations, please click on the call as follows:
We can see that the order of execution of the methods is first-level, and when there is no trigger at the previous level, it is not possible to enter the next level.
When you finish clicking StartService and Bindservice, the log and background process diagram looks like this:
As you can see, the background process starts, and the activity and service bindings succeed, and you can invoke methods in the background.
When you finish clicking Unbindservice, the onUnbind()
method is unbound, and the log and background process diagram is as follows:
As you can see, the service is not destroyed and the contents of the service can still be invoked, although it is unbound.
When the stopservice is clicked, the service is destroyed and onDestory()
the log and background process diagram is as follows:
As you can see, the service in the background has been destroyed, but the emphasis in the focus is that the methods in the service can still be called.
Summarize
There are three scenarios for opening a service: If you are using the service directly, there is no need to bind, but if you want to use the methods inside the service, you bind. In addition, the methods in the service can be invoked whenever bindservice is used, regardless of whether or not to unbind and stop the service afterwards
Android Basic Notes (16)-Service:startservice (), StopService (), Bindservice (), Unbindservice () supplement