Official Documents original Address
Application Principles
Android applications are written in the Java programming language. The Android Software development tool compiles and packages your code and other data and resource files together into an apk file, which is suffixed with an. APK to save the entire contents of an Android app. The Android device uses it to install the appropriate app.
Once installed on the device. Each Android application executes in its own separate security sandbox:
- The Android system is a multi-user Linux system. Each application is a user.
- The Android system assigns a unique user ID to each app by default (this ID is only used by the system.) Application does not understand).
The system sets permissions for each of the appropriate files for each application. Only the real user ID of the correct application talent interview.
- Each process has its own virtual machine, so an application code can be executed independently, without interference from other applications.
- By default, each application executes in its own standalone Linux process.
When a component in an application needs to be executed, the system will open its process. When there is no activity in this process or the system memory is not sufficient, the shutdown process reclaims memory for other applications. Will close the process.
Each application executes in its own separate sandbox, in such a way that the Android system implements the principle of least privilege-that is, each application has access to only the resources that it needs to work with (the translator Note: Note that this is the default.) In many cases we actually need to access resources from other applications. Another key technology, "process communication", is needed at this point. This creates a very secure environment in which an application cannot access files that it does not have permissions for.
However, the system provides some ways to share data and apply access to system services among applications:
- We were able to share the same Linux user ID for two apps so that they could access each other's files.
To conserve system resources, applications with the same UID can perform in the same Linux process, using the same virtual machine, provided that these applications must have the same signature.
- An app can ask for some access to device data. For example, the user's contacts, SMS, SD card, camera, Bluetooth and so on.
Users can decide whether to grant these permissions to the app.
The above outlines the principles of how Android applications exist in the system. The following will describe:
- Building the core framework components of your application
- declaring components and requesting device features for your app in the manifest file
- A resource that is as elegant as the code and allows your app to perform in many device configurations
Application components
Application components are the necessary modules for building an Android program. The system is able to enter your application through components of different components. Not all components users can see and feel, some components need to rely on other components ability to start, but they are also as an independent entity, and play a different role at the same time to help you decide what your application behavior.
There are four types of application components, each of which has its own unique functionality, and at the same time has a life cycle that determines how it is created and destroyed.
Here are four different types of application components:
-
Activity
An activity represents an interface. For example, an e-mail application might have a real activity for a new mailing list, another activity to create a message, and another activity to read a message. These activities work together to compose a complete email user experience. At the same time each activity is independent of each other, so other applications can invoke the above (assuming this email application agrees).
Let's say a camera app for users to share photos. Ability to start the activity that creates the message directly.
-
Services
A service is a component that executes in the background. Often used for long time-consuming operations or for cross-process operations. The service does not provide a user interface. Say. The service can play music in the background while the user is using another app. Or you can request data from the server in the background, preferably or in the context of user interaction. Other components, such as activity, can start the service. Or bind to the service to be able to interact.
-
Content Providers
Content provider manages a set of shareable application data. You can save data to a file system, SQLite database, network, or any other place where you can access persistent storage wherever you apply. With content provider, other apps can query and even change data (assuming the content provider agree). Say. The Android system provides a content provider that manages user contact information. It allows you to query the content provider's parts, and then read or write specific contact information, regardless of the application's ability to simply have permission.
The Content provider can also be used to read and write application-private data.
-
Broadcast receivers
Broadcast receivers a component that can respond to all broadcasts.
Many broadcasts are sent by the system, such as a broadcast notification when the screen is off, a broadcast when the battery is low, or a photo-completed broadcast. Apps can also create broadcasts. For example, send a broadcast to let other programs know that data is downloaded to the device. have been able to use it. Although the broadcast recipient does not have an interface, it can create a notification in the status bar to notify the user when a broadcast event occurs. Usually the broadcast recipient is just a portal to do a few other components and does very little work. Let's say you start a service to perform some operations when you receive a broadcast.
A unique part of the Android system design is the ability to launch components from other applications, regardless of the application.
Let's say you want the user to take a picture using the device's camera. There are special applications on the device that do this, and you don't need to write a camera app. Simply call the system camera app to get a picture. When you're done, you'll get back the data for the photos you want to use.
For the user, it looks like the camera is part of your app.
When the system launches a component, a separate process is started for the app (assuming the app hasn't started yet). Then instantiate some classes that you want to use. For example, your app initiates the activity of taking care of. That activity executes the process in which the camera app resides, not your app. Therefore, unlike other platform system applications, Android apps have not only a portal (no main method in Java).
Because the system performs each application in a different process and restricts access to other application files at the same time, your app cannot directly activate the components of other apps. You can, however, directly invoke the components of the Android system. To activate the components of other applications, you must indicate in your intent that the system is launching a specific component. The system will activate that component for you.
Activating components
Three of the four components: activities, services, broadcast receivers can be activated by calling intent asynchronously.
The intent is bound to invoke and called Components at execution time (which is understood to be when the request calls other components), regardless of whether the component belongs to your application.
Create a intent with the intent object. This intent can decide whether to activate a particular component or to activate a class of components. Intent can be divided into 2, explicit and implicit.
For activities and services, a intent determines the behavior to invoke (for example, to display or send something), or it may indicate a URI to use the data (the called component may need to enter some data).
Say. A intent may make a request for an activity to display a photo or open a Web page. Sometimes you may need to receive a return result when you start an activity, in which case the activity returns the result in a intent (for example, you can launch a intent that lets the user select a contact and then return information to you.) The returned intent may include a URI that points to the selected contact.
For broadcast receivers. Intent simply defines a notification to receive a broadcast (for example, to receive a low-level broadcast of a device only needs to indicate a "low" action in intent).
Additional components are –content provider and cannot be intent activated.
It is activated by a request made against the content resolver.
This content resolver handles all the direct transactions of the content provider. Therefore, the component does not need to call provider, but calls the content resolver method directly. This preserves a middle tier between content provider and component requests, which is more secure.
Here are a few different ways to activate different types of components:
- When you want to open an activity or pass new data to an activity that has already been activated. Pass a intent to startactivity () or Startactivityforresult () (when you want to receive a return result).
- When you are about to open a service or give a new service instruction to the execution. It is possible to bind to the service by passing a intent to the Starservice () method or passing a intent to Bindservice ().
- You can instantiate a broadcast by passing a intent to the Sendbroadcast (), Sendorderedbroadcast (), Sendstickybroadcast () method.
- Perform a query operation on the content provider by calling the Contentresolver query () method.
The Manifest file
Want to enable Android to launch an application component. The system needs to know if the component exists by looking at the app's Androidmanifest.xml file. Your app must declare all the components in this file in the application code root directory.
In addition to declaring application components, the manifest has done a lot of things, for example:
- Identify the user rights that your app wants to use, such as visiting the network or visiting a user's contact person.
- Declare the user's minimum mobile version number required by the application;
- Declare the hardware and software features required by the application, such as camera, Bluetooth or multi-touch screen;
- Declares that the API library (not the Android Framework interface) to use is applied. Let's say Google Maps interface.
The activities,services you use, and the content providers assumptions that are not declared in manifest, are not available to the system and therefore cannot be executed.
However, there are two options for broadcast receivers. Either declare it in manifest, or you can create it dynamically in code (creating a Broadcastreceiver object. Then call the Registerreceiver () register).
Intent filter that declares the capability of a component
As said above. You can use a intent to start activities,services,broadcast receivers. You can explicitly use the class name of the component in intent to specify the component to invoke.
The real intent, however, is the concept of implicit invocation.
An implicit intent simple description describes the type of behavior to execute (you can also pass data to the behavior that you want to invoke), and you agree to the system to find a component that can perform the behavior you require and then open it. Suppose there are multiple components that can perform the behavior described in intent, which the user needs to choose to use.
How does the system find the components of your intent call? The answer is the intent filters label of the components in the manifest file for other applications on the device.
When you declare an activity in the app's manifest, you can also add a intent filter tag to the activity that declares the activity capability, and the activity can respond to other applications. You can add a intent filter to your component by adding a label to the component's label.
For example, if you create a mail application, you need to have an activity to create a new message. You can declare a intent filter in the activity. To respond to the intent of sending (new mail). For example, this:
... > ... ... > <activity android:name="com.example.project.ComposeEmailActivity"> <intent-filter> <action android:name="android.intent.action.SEND" /> <data android:type="*/*" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application></manifest>
Suppose there are other applications that create a intent with an action of action_send. Then start with startactivity (). Your activity may be activated by the system. The user is then able to edit and send an email.
Declaring application Configuration Requirements
There are too many types of devices on the market that are equipped with Android systems. These devices have different hardware configurations. To prevent your application from being installed on a phone that does not have the configuration you require. You need to indicate the hardware and software configuration required by your application in the manifest file. Often these lives are simply hints that the mobile phone system will not be viewed, but third-party providers will be able to browse them, for example, by Google Apps. It then helps users filter apps that have additional configuration requirements when they download the app.
Say. Let's say your app requires a webcam. and requires a minimum device configuration of Android2.7 (API Level7). You need to mark this in the manifest:
... > <uses-feature android:name="android.hardware.camera.any" android:required="true" /> <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="19" /> ...</manifest>
It has been stated that devices that do not have a webcam or Android version number below 2.1 will not be able to install your app on the Google app market.
You can also declare in your app that you want to use a webcam, but it's not required.
In this case your app must set the Required property in the above tag to false. Then, during execution, check if the device supports the camera, and if not, disable camera-related operations.
Introduction to Application Resources
An Android app is not just composed of code, it also has a lot of resources other than code files, such as pictures, audio files. Or anything else related to the app display.
For example, you need to define animations, menus, styles, colors, and layout files for interactive interfaces, and so on. Using the app resource file makes it easier to update something without changing the code and replacing the resource file directly. This allows your application to adapt as much as possible to different device environments (for example, different languages or different screen sizes).
For every resource in your project. The Software development tool will define a unique integer ID for it, which you can use to refer to the corresponding resource from your code or other resource file.
For example, your app includes a picture file named Logo.png (saved in the res/drawable/directory), and the SDK generates an ID named R.drawable.logo, which you can use to refer to the logo image. Inserted into the interface.
One of the most important advantages of providing resource files that are separated from the source is that you can provide different resource files for different devices. For example, by defining the characters in the interface in XML, you can prepare different language version numbers for these characters. stored in a different file. The system will then be based on the language in the user device settings. The suffix name in the directory where the file is located is the appropriate character to display (for example, the French character stored under res/values-fr/, which displays the characters stored in this directory when the user system language is French).
Android supports many different ways of selecting resources. This choice depends primarily on the character suffix in the name of the resource directory you created in order to use different resources under different configurations. Give me a chestnut again. You should be accustomed to creating different layout files for your activity depending on the screen size or orientation of your device. When the device screen is vertical, you have a button in your layout, and when the screen is turned into a horizontal screen, you want the button to go to the horizontal screen.
To achieve a layout change with the screen orientation, you can define 2 different layouts. Then give each layout to the appropriate, correctly selected, different directory. In this way, the system will automatically load the appropriate layout according to the screen orientation.
"Fun to read official documents" 1. Butler's Choice (Android process life cycle)
An overview of the 2.Android application Fundamentals of the "Official Archive of Books"