Android basic notes (16)-Service: startService (), stopService (), bindService (), unbindService () Supplement, android learning notes
- Open door
- First
- Second
- Third
- Summary
Open door
You can enable the Service in three cases: if you use the service directly, there is no need to bind it, but if you want to use the method in the service, you need to bind it.
-
The specific startup conditions are as follows:
-
① Call
startService()
And then call
stopService()
.
-
② Separate call
bindService()
Method, and then
unbindService()
To execute the internal method of the service.
-
③ Call
startService()
And then call
bindService()
Method, and then call
unbindService()
, And finally call
stopService()
.
-
Special cases:
-
You can call the methods in the service no matter whether or not bind or stop the service.
The following describes the three startup sequence in detail.
Paste the code before explaining it:
MyService classWithout comments.
public class MyService extends Service { @Override public IBinder onBind(Intent intent) { return new MyBinder(); } @Override public void onCreate() { System.out.println("MyService onCreate():Called by the system when the service is first created"); super.onCreate(); } @Override public boolean onUnbind(Intent intent) { System.out.println("MyService onUnbind():Called when all clients have disconnected from a particular interface published by the service."); return super.onUnbind(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { 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 is 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 { private MyBinder myBinder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); conn = new MyServiceConnection(); } ServiceConnection conn; public void start(View v) { Intent service = new Intent(this, MyService.class); startService(service); } public void bind(View v) { Intent service = new Intent(this, MyService.class); bindService(service, conn, Context.BIND_AUTO_CREATE); } public void unbind(View v) { unbindService(conn); } public void stop(View v) { Intent service = new Intent(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.xmlPage 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=".MainActivity" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="start" android:text="startService" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="bind" android:text="bindService" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="unbind" android:text="unbindService" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="stop" android:text="stopService" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="callmethod1" android:text="call Method1" /></LinearLayout>
First
CallstartService()
And then callstopService()
. This situation applies to direct use of the Service and does not require external calls to internal methods of the Service.
In this method, we will click startService and stopService respectively. In the MyService classonCreate()
Start-indicates the first time a service is created.onDestory()
End: indicates that the service is destroyed. The service is called once or multiple times (when startService is repeated)onStartCommand()
Method-to indicate the clientClear.
When you click startServiceonCreate()
AndonStartCommand()
Method, indicating that the service is created for the first time and is explicitly required by the client. After the two methods are executed, the backend service thread starts.
Take a look at this figure:
The Log diagram corresponding to this process and the application background service process diagram are as follows:
You can clearly see thatonCreate()
AndonStartCommand()
And the background service process is started.
When you click stopServiceonDesctory()
In this case, the background service process is destroyed.
Take a look at this figure:
The Log diagram corresponding to this process and the application background service process diagram are as follows:
You can clearly see thatonDesctory()
And the background service thread is destroyed.
Second
Separate callbindService()
Method to bind the Activity and Service to achieve the purpose of internal Service methods, and then callunbindService()
Unbind.
In this method, we will click bindService and unbindService respectively. In the MyService classonCreate()
Start-indicates the first time a service is created.onDestory()
End-indicates that the service is destroyed. In the middle, when the binding is successfulonServiceConnected()
Indicates that the Activity and Service are successfully connected. When unboundonUnbind()
The Activity and Service are successfully unbound.
When you click bindServiceonCreate()
AndonServiceConnected()
Method To Call The internal method of the service.HoweverThe background service process is not started.
Take a look at this figure:
The Log diagram corresponding to this process and the application background service process diagram are as follows:
You can clearly see thatonCreate()
AndonServiceConnected()
Method,However, the background service process is not started.. After binding, you can call the internal method of the service.MyService is method1
Is proof.
When you click unbindServiceonUnbind()
AndonDestory()
Method indicates to unbind and destroy the service.
Take a look at this figure:
The Log diagram corresponding to this process and the application background service process diagram are as follows:
You can clearly see thatonUnbind()
AndonDesctory()
Method, and there are no service threads in the background.However, although the binding is unbound, we can still call methods in the service.
Third
CallstartService()
And then callbindService()
Method, and then callunbindService()
, And finally callstopService()
This situation applies to the hope that the service can run for a long time in the background, as long as the service is not stopped, the Activity can also call the methods in the service.
When using four combinations, click the following method:
We can see that the sequence of execution methods is Level 1. When the upper level is not triggered, it cannot enter the lower level.
After you click startService and bindService, the Log and background processes are shown as follows:
You can see that the background process is started, and the Activity and Service are successfully bound, and you can call the background method.
When unbindService is clickedonUnbind()
Method To unbind. The Log and background process diagram is as follows:
As you can see, although unbound, the service is not destroyed,The content in the service can still be called.
When stopService is clicked, the service is destroyed.onDestory()
The Log and background processes are shown as follows:
We can see that the background services have been destroyed,However, the focus is that the methods in the service can still be called.
Summary
You can enable the Service in three cases: if you use the service directly, there is no need to bind it, but if you want to use the method in the service, you need to bind it. In addition, you can call the methods in the service no matter whether or not to unbind or stop the service.