Android Development Note 2 framework

Source: Internet
Author: User
Tags sqlite database

One: Layering of frames

The Android system architecture consists of 5 parts, namely: Linux Kernel, Android Runtime, Libraries, application Framework, applications

Direct comparison of Android and. NET (Winfrom)

In order not to let friends come in and see addlebrained, this picture I simplified a lot, briefly introduced:

Linux kernel: A driver that manages your phone's hardware, including camera drivers, bluetooth drivers, wifi drivers, and more, for upper-level calls. You're right, Android is a sophisticated Linux kernel.

C + + libraries: Libraries for C/s + +, including Webkit,opengl (see friends who like to play games here), the most important thing is to include a sqllite, you do not read wrong, like early adopters. NET friends have played the most popular embedded database, in your mobile phone is lying. Your contacts, the text messages, all exist in this one. (to the sister's contact AH SMS Ah, what bad idea, well, to trust each other.) )

Android Runtime : The most important thing here is the Dalvik virtual machine . We know that the Java application runs on the JVM virtual machine (equivalent to the CLR runtime), obviously the power consumption of the phone and the PC can not be compared, so Google has done a lot of optimizations to the JVM, so the birth of the Dalvik virtual machine, the original Java application run in the way: xx.java- >XX.CLASS->JVM execution, now the Java application is executed as follows: Xx.java->xx.class->xx.dex->dalvik execution. This is a few people in mind, although we are quick Android, but also quality and quantity:)

Android Framework: Android provides a range of Java system services for applications to invoke to implement functionality.

II: Application Basics

The compiled Java code-including any data and resource files required by the application, is bundled into an Android package via the AAPT tool, and the archive file is suffixed with an. apk. This file is an intermediary or tool for distributing applications and installing to mobile devices, and users download this file onto their devices. All the code in an. apk file is considered an application.

AAPT:

AAPT is the acronym for the Android Asset Packaging tool, which is included in the tools/directory of the SDK. View, create, and update zip-compatible archive files (zip, jar, apk). It can also compile the resource file into a binary package.

Although you may not often use appt directly, the build script (build scripts) and IDE plugin will use this tool to package the APK file to form an Android application.

For more detailed use of the details, open a terminal, enter the tools/directory, run the command:

  • Linux or Mac operating system:./aapt
  • Windows:aapt.exe

Note: The tools/directory refers to the/platforms/android-x/tools/in the Android SDK directory

      • By default, each application is assigned a unique Linux user ID. The permissions set for each application's files are visible only to the user and the application itself-although there are also ways to expose them to other applications.
      • Each application has his own Java Virtual machine (VM), so the application code runs independently of the code of all other applications.
Three Application Components

One of the main features of Android is that an application can take advantage of the elements of other applications (assuming these applications allow it). For example, if your application needs to display a scrolling list of images, and other applications have developed a suitable scroll bar that can be provided to other applications, you can call this scroll bar to work without having to develop one yourself. Your application does not have to be incorporated into other application code or linked to it. Instead, it just launches other application blocks when the demand is generated.

For this job, when any part of the application is requested, the system must be able to start the process of an application and instantiate the Java object for that part. Therefore, unlike most other system applications, Android applications do not have a single entry point (for example, no main () function).

the system is able to instantiate and run several necessary components . There are four types of components:

    1. Activities (Activities)
    2. Service (services)
    3. Broadcast recipient (broadcast receivers)
    4. Content provider (contents providers)

However, not all applications must contain the four sections above, and your application can be organized by one or more of the above. When you decide which of these components to use to build Android apps, you should list them in the Androidmanifest.xml file, where you can declare application components and their features and requirements.

3.1 , activity (activities)---(feel similar to each of the forms in WinForm)

An activity represents a visual user interface that focuses on the events that a user is engaged in.

For example, an activity might represent a list of menu items that a user can select, or it might display a photo along with its caption. A text messaging application may have an activity that displays a list of contacts to send messages, a second activity that writes messages to selected contacts, other activities, re-viewing old information, or changing settings. While they work together to form a holistic user interface, each activity is independent of other activities. Each is implemented as a subclass of the activity base class.

Android.app.Activity class: Because almost all activities (activities) interact with the user, the activity class focuses on creating Windows, and you can use the method to setContentView(View) put your own UI inside. However, activities are usually presented to the user in full-screen mode, or in a floating window or embedded in another activity. There are two methods that are implemented by almost all activity subclasses:

  1. onCreate(Bundle):初始化你的活动(Activity),比如完成一些图形的绘制。最重要的是,在这个方法里你通常将用布局资源(layout resource)调用setContentView(int)方法定义你的UI,和用findViewById(int)在你的UI中检索你需要编程地交互的小部件(widgets)。setContentView指定由哪个文件指定布局(main.xml),可以将这个界面显示出来,然后我们进行相关操作,我们的操作会被包装成为一个意图(Intent),然后这个意图对应有相关的activity进行处理。
  2. onPause(): Deal with what you have to do when you leave your activity. Most importantly, all changes made by the user should be committed here (usually ContentProvider saving the data).

An application may contain only one activity, or, like the SMS app just mentioned, it may contain several activities. What these activities are, and how much, of course, depends on its application and design. In general, when an application is started, the activity that is marked as the first should be displayed to the user. Moving from one activity to another activity is done by the current activity next. (similar to the Load event for forms in WinForm, which is equivalent to the Activiry event in Android)

An application may contain only one activity, or, like the SMS app just mentioned, it may contain several activities. What these activities are, and how much, of course, depends on its application and design. In general, when an application is started, the activity that is marked as the first should be displayed to the user. Moving from one activity to another activity is done by the current activity next. (particularly similar to the individual events in WinForm)

The visual content of a window is provided by a hierarchical view-object that inherits from the view base class. Each view control is a specific rectangular space within the window. Therefore, a view is where the activity interacts with the user. For example, a view might show a small picture and initiate a behavior when the user clicks on the picture. Android has some out-of-the-box views you can use, including buttons (buttons), text fields, scroll bars (scroll bars), menu items (menu items), checkboxes (check boxes), and more.

Place a view hierarchy in an active window by using the Activity.setcontentview () method. Content view isThe root View object of the hierarchy. The hierarchy looks like this:

3.2 , Service (services)

A service does not have a visual user interface, but runs in the background with no deadlines. For example, a service may be playing background music while the user is doing something else, or it may fetch data from the network, or calculate something and provide the result to the desired activity (activities). Each of the services inherits from the service base class.

Each service class has a corresponding <service> declaration in Androidmanifest.xml. Services can be started by Context.startservice () and Context.bindservice ().

A typical example is a media player playing a playlist of songs. The player application will likely have one or more activities (activities) that allow the user to select songs and start playing. However, the music playback itself is not handled by an activity because the user wants to keep the music playing and when the user leaves the player to do other things. In order to keep the music playing, the media player activity can start a service running in the background. The system will keep the music playback service running, even when the media player leaves the screen.

3.3 broadcast recipient (broadcast receivers)

A broadcast receiver is such a component that it does nothing but receives a broadcast announcement and responds accordingly. Many broadcasts originate from system code, such as the announcement of time zone changes, low battery power, pictures taken, and user-changed language preferences. Applications can also initiate broadcasts, for example, in order for other programs to know that certain data has been downloaded to the device and that they can use the data.

An application can have any number of broadcast receivers to react to any announcements it deems important. All recipients inherit from the Broadcastreceiver base class.

Broadcastreceiver class:

is the base class that accepts the intent (intents) sent by the Sendbroadcast (). You can use Context.registerreceiver () to dynamically register instances of this class, or to publish statically via the <receiver> tab in Androidmanifest.xml. Note: If you register a recipient in Activity.onresume (), you should unregister it in Activity.onpause (). Because when you pause you will not receive the intention to unregister it will reduce unnecessary system overhead. Do not unregister it in activity.onsaveinstancestate () because it will not be called if the user moves to the previous stack.

There are two main acceptable broadcast types:

  1. Normal broadcasts (sent by context.sendbroadcast) are completely asynchronous. All broadcast receivers run in an unordered fashion and are often received at the same time. This is more efficient, but it means that the recipient cannot use the results or terminate broadcast data propagation.
  2. ordered broadcasts (sent by Context.sendorderedbroadcast) are passed to one recipient at a time. Because each recipient executes sequentially, it can propagate to the next sink, or it can completely terminate the propagation so that he does not pass it on to other receivers. The order in which the receivers are run can be controlled by the properties of the matching intent filter (Intent-filter) android:priority .

The broadcast recipient does not display a user interface. However, they initiate an activity to respond to received information, or they may use NotificationManager to notify the user. Notifications can be used in a variety of ways to get the user's attention-flashing backlighting, vibrating devices, playing sounds, and more. Typically placed on a persistent icon in the status bar, the user can open the get information.

3.4. Content provider (contents providers)

Content provider provides the specified dataset for an application to other applications. This data can be stored in a file system, in a SQLite database, or in any other reasonable manner. The content provider inherits from the ContentProvider base class and implements a standard set of methods that enable other applications to retrieve and store data. However, applications do not call these methods directly. Instead, they are replaced by methods that use a Contentresolver object and invoke it. Contentresolver can communicate with any content provider, which works with providers to manage communication between participating processes.

Content providers are one of the main components of Android applications that provide content to applications. They encapsulate the data and are available to the application through a single Contentresolver interface. You need content providers only if you need to share data among multiple applications. For example, Address book data is used by multiple applications and must be stored in a content provider. If you don't need to share data among multiple applications, you can use Sqlitedatabase directly.

When Contentresolver makes a request, the system checks the permissions of the given URI and passes the request to the content provider for registration. The content provider can understand what the URI wants. The Urimatcher class is used to help group parse URIs.

The main methods to be implemented are as follows:

  • query(Uri, String[], String, String[], String)Returns data to the caller
  • insert(Uri, ContentValues)Inserting data into a content provider
  • update(Uri, ContentValues, String, String[])Update data that the content provider already exists
  • delete(Uri, String, String[])Delete data from the content provider
  • getType(Uri)Returns the MIME type data in the content provider
3.5. Activating components: Intent (Intents)

When a request is received from the contentresolver , the content provider is activated. The other three components- activity, service, and broadcast receivers -are activated by an asynchronous message called intent (intent) .

The intent is a intent object that holds the content of the message, and for activities and services , the intent object indicates the requested operation name and the URI and other information that is the data of the manipulation object. For example, it can pass a request to an activity, let it display a picture for the user, or let the user edit some text. for broadcast receivers , the intent object indicates the behavior of the broadcast. For example, when the camera button is pressed, it can broadcast to all objects of interest.

3.5.1, activation of the active (activity) component

An activity is loaded (or assigned a new job) by passing a intent object to context.startactivity () or Activity.startactivityforresult (). The corresponding activity can see the initial intent, and this intent is to view the activation activity through the Getintent () method. Android calls the active Onnewintent () method to pass any subsequent intent.

An activity often starts the next one. If it expects the activity it initiates to return a result, it calls Startactivityforresult () instead of startactivity (). For example, if it launches an activity that lets the user pick a photo, it may return the selected photo. The result is a intent object that passes the Onactivityresult () method that invokes the activity.

3.5.2, Activation of service components

Start a service (or give a new instruction to a running service) by passing a intent object to Context.startservice (). Android invokes the OnStart () method of the service and passes the intent object to it.

Similarly, a intent can be passed to Context.bindservice () to establish a continuous connection between the called component and the target service. This service will accept the intent object in the call to the Onbind () method (if the service has not started, Bindservice () will start it first). For example, an activity can connect to the music playback service described earlier and provide the user with an actionable (user interface) to control playback. This activity can call Bindservice () to establish a connection, and then invoke the object defined in the service to control playback.

3.5.3, activation of the broadcast receiver (broadcast receiver) component

An application can pass a intent object to the

    • Context.sendbroadcast ()
    • Context.sendorderedbroadcast ()
    • Context.sendstickybroadcast ()

and other similar methods to generate a broadcast. Android passes intent through the OnReceive () method to all broadcast recipients who are interested in this broadcast.

3.6. Turn off the components
    • You can close an activity by calling its finish () method. An activity can also close another activity (it starts with Startactivityforresult ()) by calling the Finishactivity () method.
    • The service can stop by calling its Stopself () method, or call Context.stopservice ().
3.7. manifest (manifest) file

The manifest file is a file of XML structure, and all Android applications call it androidmanifest.xml. To declare an application component, it also does a lot of extra work, such as specifying the name of the library that the application needs to link to (in addition to the default Android library) and declaring the various permissions that the application expects to get.

But the main feature of the manifest file is still the component that declares the application to Android. For example, an activity can be declared as follows:

<?xml version="1.0"encoding="Utf-8"><manifest ... > <application > <activity android:name="com.example.project.FreneticActivity"Android:icon="@drawable/small_pic.png"Android:label="@string/freneticlabel"                    . . .  > </activity>          . . .      </application></manifest>

The Name property of the <activity> element specifies the subclass of the activity class that implements the activities, and the icon and label properties point to the resource file that contains the icon and label for this activity that is presented to the user.

Other components also declare the--<service> element in a similar way to declare the service,<receiver> element is used to declare the broadcast recipient, while the <provider> element is used to declare the content provider. Activities, services, and content providers that are not declared in the manifest file will not be visible to the system and thus will not be run. However, broadcast receivers can be declared either in the manifest file or dynamically in code (as BroadcastReceiver objects) and called Context.registerreceiver () to register to the system.

3.8 Intent Filter

The intent object can explicitly specify the target component. If this designation is made, Android will find the component (according to the declaration in the manifest file) and activate it. But if intent is not explicitly specified, Android must find the component that is most appropriate for intent. This process is done by comparing the intent object with the intent filter for all possible objects . The intent filter for the component tells Android what type of intent it can handle. They are declared in the manifest file, just like other necessary information about the component. Here is an extension of the above example, which includes two intent filter declarations for the activity:

<?xml version="1.0"encoding="Utf-8"><manifest ... > <application > <activity android:name="com.example.project.FreneticActivity"Android:icon="@drawable/small_pic.png"Android:label="@string/freneticlabel"                    . . .  > <intent-filter > <action android:name="Android.intent.action.MAIN"/> <category android:name="Android.intent.category.LAUNCHER"/> </intent-filter> <intent-filter ... > <action android:na Me="Com.example.project.BOUNCE"/> <data android:mimetype="Image/jpeg"/> <category android:name="Android.intent.category.DEFAULT"/> </intent-filter> </activity>          . . .      </application></manifest>
The first filter in the example--action: The combination of "Android.intent.action.MAIN" and Category: "Android.intent.category.LAUNCHER" is common. It marks the list of bootable applications that the activity displays in the Application Launcher, which the user sees on the device. In other words, this activity is the portal to the application and is the first activity that the user sees when they choose to run the application. The second filter declares that the activity is for a specific type of data.

A component can have any number of intent filters, each declaring a range of different capabilities. If it does not contain any filters, it will only be explicitly declared with intent activation of the target component name.

For broadcast receivers, it creates and registers a intent filter in code that is instantiated directly as an object of Intentfilter. Other filters are set in the manifest file.

Android Development Note 2 framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.