Learning about the basics of Android from 0 (1)-four basic components of Android
Introduction to four basic components of Android applications
Android applications are generally composed of four parts, including Activity Service BroadcastReceiver ContentProvider. This blog will provide a basic introduction to these four components, so that you can have a general understanding of these components, the following blogs will also explain them in detail.
ActivityActivity is the component in the android app that is responsible for interacting with users. It is the component that presents the interface of our android mobile phone. You can think of it as a photo frame, the pictures presented by our android app are in this photo frame, and the size of this photo frame is the size of our mobile phone screen. He presents images to our users and accepts a series of response events, such as click sliding. After we design the interface (XML file) to be presented by the android Application, the activity can only display the specified interface through setContentView (View). For example, after we take a picture, select the photo frame to use to load the photo. Therefore, bind the display page for the activity. SetContentView (View) is generally in the following format:
setContentView(R.layout.main);
Here, R. layout. main is a layout file in the android resource file, which is equivalent to a photo. Note that, in fact, Actvity is the container of Window. Activity contains a getWindow () method. This method returns the Window contained in the Activity. For Activity, as a developer, we do not need to care about its window object. If the application does not need to call the setContentView () method of Activity to set the display interface of the activity, the application will display an empty window. An android app contains many activities, multiple of which form an Activity stack. The storage method is advanced and later, just like putting a book in a box, putting the book first at the bottom, finally, the running activity is located at the top of the stack. Another point is that our Activity can design its own style, that is, the topic. You can use setTheme (int resouceId) to specify the activity style, for example, if our activity does not need to be displayed in actionBar or dialog box, you can use this method or in AndroidManifest. xml specifies the display style of the activity. 2.
Service
ServiceIt is as important as activity. It only does not display the interface. The service usually runs in the background. Because there is no interface display, it generally does not need to interact with the user. The Service component must inherit the basic Service class. After the Service runs, it has its own independent lifecycle. The Service component is usually used to provide backend services for other components or listen to the running status of other components. A typical application is the music player. The home page is the interface on which the activity is presented to the user. Services are used to play music in the background. The Service then notifies the Activity to update the interface. The pause button of the Activity is clicked by the user, and then the Service is notified to stop or play music. 3.
BroadcastReceiver
BroadcastReceiver is an android application.According to this name, we certainly know that it is actually a broadcast message receiver. What is different from a common listener is: objects in the event source program listened by a common listener, while the event source listened by BroadcastReceiver is other components in the Android Application (activity aggreger ). It is relatively easy to use the broadcastReceiver component to receive broadcast messages. We only need to implement our own BroadcatReceiver subclass and rewrite the onRecerver (Context context, Intent intent) method. When other components send a broadcast message through the sendBroadcast (), sentStickyBroadcast (), or sentOrderBroadcast () method, if the BroadcastReceiver is interested in the message, configure it through IntentFilter (this is a filter, set some conditions. If a message matches these conditions, the BroadcastReceiver is activated. The onReceive () method of BroadcastReceiver is activated, and some actions set in this method are executed. After playing the music in the Service, it sends a message to the activity through sendBroadcast (). After the activity receives the message, it executes the onReceive () method in BroadcastReceiver, the activity will update the interface accordingly. BroadcastReceiver can be started only after registration. Otherwise, no message is sent or received. The following two methods are available: (1). Register BroadcastReceiver using the Context. registReceiver () method in java code; (2). Use it in AndroidManifest. xml Complete element registration; Here, I will only give a brief introduction to BroadcastReceiver. Later I will explain it in detail and teach you how to develop and use it. 4.
ContentProviderFor Android applications, they run independently in their Dalvik Virtual Machine instances. If these Android applications need to exchange real-time data, contentProvider is required to complete this work, it is used to obtain mobile phone contacts. ContentProvider is a standard for cross-application data exchange in Android. When users implement their own ContentProvider, the following four methods are implemented: (1) insert (Uri, ContentValues): insert data to ContentProvider; (2) delete (Uri, ContentValues): delete the data specified in contentProvider; (3) update (Uri, ContentValues, String, String []): update the specified ContentProvider in ContentProvider; (4) query (Uri, String [], String, String [], string): queries data from ContentResolver. When ContentProvider is used, ContentResolver is used in combination. One Application exposes its own data to the outside, and the other uses ContentResolver to access data. 5.
Intent and IntentFilter
Intent application in androidAnd has a very high role. It is the carrier for communication between different components in Android applications. Intent is required when different components need to be linked during android runtime. Intent can start an Activity, a Service, or broadcast messages to trigger BroadcastReceiver in the system. That is to say, intent is the carrier for communication between the three components Activity, Service, and BroadcastReceiver, but they use different Intent methods. (1) When you need to start an activity, you can call the startActivity (Intent intent) or startActivityForResult (Intent intent, int requestCode) method of Context, both methods encapsulate the information of the target activity to be started. (2) when you need to start a Service, you can call startService (intent Intent) of Context) alternatively, the bindService (Intent service, ServiceConnection conn, int flags) method encapsulates the information of the target service to be started. (3) When a BroadcastReceiver needs to be triggered, you can call sendBroadcast (Intent intent) or sendStickyBroadca of Context. The st (Intent intent) or sendOrderBroadcast (Intent intent, String receiverPermission) method sends broadcast messages. The intent encapsulates the information of the target BroadcastReceiver to be triggered; from the above introduction, we can see that Intent encapsulates the information of the target to be started or triggered by the current component. However, Intent is divided into two categories: (1) display Intent: the Intent explicitly specifies the Class Name of the component to be started or triggered; (2) Implicit Intent: implicit Intent only specifies the conditions that need to be met for the component to be started or triggered. For Intent display, Android does not need to parse the Intent, the system directly finds the specified target component and triggers it upon startup. For implicit Intent, the Android system needs to parse the Intent and parse its conditions, then go to the system to find the matched items Component. Start or trigger a component that meets the conditions. Implicit Intent is implemented through IntentFilter. The called component can use IntentFilter to declare the conditions it meets, that is, the Intent that it can process. Intent and IntentFilter have a lot of content, which will be detailed in later blogs. This blog introduces so much.