About Service:
For Android applications, due to the limitations of mobile phone screen and hardware resources, usually only one application is allowed to be in the active state, presenting to users and exchanging information with users, while other applications are all inactive. But in many practical applications, such as MP3 players, the application needs to work even if the user interface is not displayed. When users operate in the MP3 user interface and select MP3 to play, they usually directly exit the interface and continue to use other applications of the mobile phone, such as surfing the Internet and chatting while listening to music. In order to meet the above user requirements, the Android system provides the Service component. We can realize the music playing function in the Service component.
A Service is a Service component of the Android system. It is suitable for developing functions that have no user interface but need to be run in the background for a long time. These functions usually include music playback, network data acquisition, time-consuming computing and so on. A Service is typically started by an Activity component, but it does not depend on an Activity. A Service has its own life cycle. Even if the Activity that started it is destroyed, the Service can continue to run until the end of its life cycle.
The use of the Service Service, you can refer to this article: blog.csdn.net/qq15577969/…
First, define an internal broadcast class (broadcast receiver) in mainActivity to receive the information returned by the service
/** * defines a broadcast receiver that implements the requirements of the Service Service (inner class) */
private class ServiceNeedBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Here is the code to execute in the Activity}}Copy the code
Declare two variables in MainActivity and register an instance of the broadcast receiver at initialization
// Declare an operation constant string
public static final String ACTION_SERVICE_NEED = "action.ServiceNeed";
// Declare an internal broadcast instance
public ServiceNeedBroadcastReceiver broadcastReceiver;
/** * Register the broadcast instance (at initialization time) */
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_SERVICE_NEED);
broadcastReceiver = new ServiceNeedBroadcastReceiver();
registerReceiver(broadcastReceiver, filter);
Copy the code
Send a broadcast message to the Activity in the Service class
public class taskService extends Service {
/** Callback when startService() is called to start the service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Sends broadcast messages to the Activity interface in the Service class
Intent intentBroadcastReceiver = new Intent();
intentBroadcastReceiver.setAction(MainActivity.ACTION_SERVICE_NEED);
sendBroadcast(intentBroadcastReceiver);
return super.onStartCommand(intent, flags, startId);
}
/** Bind to the client of the service */ via bindService()
@Override
public IBinder onBind(Intent intent) {
return null; }}Copy the code