Chapter 10th Service and Broadcastreceiver
10.1 Service Introduction
Service components are also executable programs that have their own lifecycle, and the process of creating, configuring, and configuring the service is essentially similar to the procedure for creating and configuration activity. The service has been running in the background without a user interface.
10.1.1 Creating and configuring a service requires 2 steps: 1 defines the subclass of the base service, and 2 configures the service in the Androidmanifest.xml file.
service and activity are derived from the context , so you can call the context method: GetResource (), Getcontentresolver (), and so on.
The service also defines a series of life-cycle methods, similar to activity.
The 10.1.2 start and stop service is started through the StartService () method of the context and is closed by the StopService method.
Starting an existing service component multiple times will no longer callback the OnCreate method, but will callback the Onstartcommend method each time it is started.
10.1.3 bind the ground service and communicate with it through the Bindservice method of the context to start, close by Unbindservice method
The service class developed must provide a ibinder Onbind (Intent Intent) method that returns a IBinder object that is passed to the Serviceconnection object.
The IBinder object is equivalent to the internal hook or proxy object of the service component , and other program components access the data inside the service through the IBinder object, enabling real-time communication between the client and the service.
In development, it is common to implement your own IBinder objects in a way that inherits binders (IBinder implementation classes).
( source \10\10.1\bindservice)
The life cycle of the 10.1.4 service
If you call Bindservice () to bind a started service, the system only passes the IBinder object inside the service to the activity and does not bind the service life cycle to the activity. Therefore, when the activity is called unbindservice (), it simply disconnects the activity from the service and does not stop the service component.
10.1.5 uses Intentservice, which is a subclass of service that adds extra functionality
Service Problem : Service does not start a separate process specifically, the service is in the same process as the application it is in, the service is not a new thread, and it should not handle time-consuming tasks in the service.
Intentservice just can make up for the above two deficiencies of service. Intentservice opens a worker thread for each intent request to process the intent.
When all request processing is complete, Intentservice automatically stops, so the developer does not need to call the method to stop the service.
The extended Intentservice implementation service does not need to override the Onbind, Onstartcommand methods, as long as the Onhandleintent method is overridden.
( source \10\10.1\intentservicetest)
10.2 Calling service (Aidl service) across processes
To achieve cross-process communication (IPC), Android provides a aidl Service.
10.2.1 Aidl Service Introduction
10.2.2 Creating a Aidl file
10.2.3 exposing the interface to the client
10.2.4 Client Access Aidl Service
10.3 Phone Manager (Telephonemanager)
( source \10\10.3\telephonystatus get network and SIM card information )
10.4 SMS Management (Smsmanager)
10.5 Audio Manager (Audiomanager)
10.6 Vibrator (Vibrator)
10.7 Mobile phone Alarm Service (Alarmmanager)
10.8 Receiving broadcast messages
Broadcastreceiver is essentially a global listener that makes it easy to communicate between different components in the system.
10.8.1 Broadcastreceiver Introduction
10.8.2 Send Broadcast
( source \10\10.8\broadcast)
10.8.3 Orderly Broadcasting
10.9 Receiving system broadcast messages
( source \10\10.9\launchservice boot auto-run service)
( source \10\10.9\monitorsms SMS Reminder )
( source \10\10.9\monitorbattery phone battery tips )
13th Android Web Application
Android fully supports the TCP, UDP network communication API of the JDK itself, and can also use ServerSocket, sockets to establish TCP/IP protocol-based network communication.
13.1.3 using sockets for communication
( source \13\13.1\simpleclient)
13.2 using URLs to access network resources
( source \13\13.2\getposttest)
13.3 Accessing the network using HTTP
13.3.1 using HttpURLConnection
13.3.2 using Apachehttpclient
13.4 Displaying Web pages using the WebView view
13.4.1 using WebView to browse the Web
13.4.2 loading HTML code with WebView
13.4.3 using JavaScript in WebView to call the Android method