Joke Collection Website - Blessing messages - Android Lecture 6 Broadcast Receivers and Services

Android Lecture 6 Broadcast Receivers and Services

There are two methods: static registration and dynamic registration.

Dynamic registration:

1) dynamic registration: you need to define a subclass that inherits from the BroadcastReceiver class, and the receiver needs to log off in the active onDestroy.

2) static registration: by configuring in AndroidManifest.xml

There are two forms of broadcasting: orderly broadcasting and disorderly broadcasting.

1) Out-of-order broadcast: The receiver receiving the standard broadcast will receive the broadcast message at the same time, which will be executed asynchronously instead of sending the broadcast in sequence.

2) Ordered broadcast: SendOrdered broadcast is accepted in a certain order, which is determined by the priority attribute. The broadcast interrupted the broadcast.

If you only want to send and receive broadcasts in this application, use LocalBroadcastReceiver to manage broadcasts.

Local broadcasting does not support static registration.

Advantages: safe and efficient.

Service is a component in Android, which is consistent with the activity level, but it can't run by itself, only in the background, interacting with other components, and the service must be registered before it can be used.

Local service: This service is attached to the main thread, saving resources. When the main thread dies, the service will also be terminated.

Remote service: the service is in an independent process, with good flexibility and high resource occupation.

Start modes of two services:

1) startup mode: the caller is not associated with the service, and the caller's exit will not affect the service. StartService starts the service. If the service does not exist, call the onCreat method, and then call onStartCommand. StopService closes the service and calls the onDestroy method.

2) binding mode: the caller binds to the service, the caller exits, the service terminates, the bindService starts the service, the onCreate method creates the service, the onBind method binds the service, the onbind method unbinds, and the onDestory is called at the end of the service.