What is Service
Service is an application component in an Android system. It is about the same level as Activity, but it does not have a graphical interface, cannot run it by itself, can only run it in the background, and can interact with other components such as updating ContentProvider, Intent, and system notifications, etc. There are two ways to start: () and (). Service is usually used to handle some time-consuming operations.
Service writing
Create a class (Here FirstService) inheritance and override the following methods:
onBind(Intent intent) Return the communication channel to the service.
onCreate() Called by the system when the service is first created.
onStartCommand(Intent intent, int flags, int startId) Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.
onDestroy() Called by the system to notify a Service that it is no longer used and is being removed.
Add service configuration to the file
<service android:name=".FirstService"></service>
Start and stop writing of click events for Service in Activity
class StartServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
(, );
startService(intent);
}
}
class StopServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
(, );
stopService(intent);
}
}
Service is an application component in an Android system. It is about the same level as Activity, but it does not have a graphical interface, cannot run it by itself, can only run it in the background, and can interact with other components such as updating ContentProvider, Intent, and system notifications, etc. There are two ways to start: () and (). Service is usually used to handle some time-consuming operations.
Service writing
Create a class (Here FirstService) inheritance and override the following methods:
onBind(Intent intent) Return the communication channel to the service.
onCreate() Called by the system when the service is first created.
onStartCommand(Intent intent, int flags, int startId) Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.
onDestroy() Called by the system to notify a Service that it is no longer used and is being removed.
Add service configuration to the file
Copy the codeThe code is as follows:
<service android:name=".FirstService"></service>
Start and stop writing of click events for Service in Activity
Copy the codeThe code is as follows:
class StartServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
(, );
startService(intent);
}
}
class StopServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
(, );
stopService(intent);
}
}