Overview of GUIs for Embedded Applications
Characteristics of Interaction Modalities of Android Devices
Screens of Various Sizes, Densities, and Specifications
Keypads and Special Keys
android.view.KeyEvent
classes. Figure 1-1’s button/key callouts correspond to the event information listed in Table 1-1.
android.view.KeyEvent
for details. Table 1-1’s contents are excerpts.Key/Button | Key Code | Another Name | Key Event |
---|---|---|---|
Key ① in Figure 1-1
| 24 | KEYCODE_VOLUME_UP
| {action=0 code=24 repeat=0 meta=0 scancode=115 mFlags=8}
|
Key ② in Figure 1-1
| 25 | KEYCODE_VOLUME_DOWN
| {action=0 code=25 repeat=0 meta=0 scancode=114 mFlags=8}
|
Key ③ in Figure 1-1
| 82 | KEYCODE_MENU
| {action=0 code=82 repeat=0 meta=0 scancode=139 mFlags=8}
|
Key ④ in Figure 1-1
| No response | ||
Key ⑤ in Figure 1-1
| 4 | KEYCODE_BACK
| {action=0 code=4 repeat=0 meta=0 scancode=158 mFlags=8}
|
Key ⑥ in Figure 1-1
| No response | ||
A–Z | 29–54 | KEYCODE_A –KEYCODE_Z
| |
0–9 | 7–16 | KEYCODE_0 –KEYCODE_9
| |
Key ⑨ in Figure 1-1
| 19 | KEYCODE_DPAD_UP
| |
Key 11 in Figure 1-1
| 20 | KEYCODE_DPAD_DOWN
| |
Key 12 in Figure 1-1
| 21 | KEYCODE_DPAD_LEFT
| |
Key 10 in Figure 1-1
| 22 | KEYCODE_DPAD_RIGHT
| { action=ACTION_DOWN, keyCode=KEYCODE_DPAD_RIGHT, scanCode=106, metaState=0, flags=0x8, repeatCount=0,
eventTime=254791, downTime=254791, deviceId=0, source=0x301 }
|
Key 13 in Figure 1-1
| 23 | KEYCODE_DPAD_CENTER
| { action=ACTION_DOWN, keyCode=KEYCODE_DPAD_CENTER, scanCode=232, metaState=0, flags=0x8, repeatCount=0,
eventTime=321157, downTime=321157, deviceId=0, source=0x301 }
|
Key ⑦ in Figure 1-1
| 5 | KEYCODE_CALL
| { action=ACTION_DOWN, keyCode=KEYCODE_CALL, scanCode=231, metaState=0, flags=0x8, repeatCount=0, eventTime=331714,
downTime=331714, deviceId=0, source=0x301 }
|
Key ⑧ in Figure 1-1
| 6 | KEYCODE_ENDCALL
|
Touch Screens and Styluses, in Place of Mice
Onscreen Keyboards
Few Multimodal Interactions
Few Large-Capacity Portable External Storage Devices
UI Design Principles for Embedded Systems
Considerations of Screen Size
Size of Text and Icons
res
directory, such as drawable-hdpi
, drawable-mdpi
, and drawable-xhdpi
.Clickability of Buttons and Other Graphical Elements
Size of Application Windows
Considerations Arising from Touch Screens and Styluses
Correctly Interpreting the Movement and Input of the Cursor (Mouse) on Tap-Only Touch Screens
Setting Screen Mapping Correctly
How to Solve Hover-Over Problems
Providing Right-Click Functionality
Keyboard Input Problems
Restricting the Input of Various Commands
Meeting Keyboard Demand
Software Distribution and Copyright Protection Problems
Android Application Overview
Application File Framework
E:\Android Dev\workspace\HelloAndroid>TREE /F
E:.
│ .classpath
│ .project
│ AndroidManifest.xml
│ ic_launcher-web.png
│ proguard-project.txt
│ project.properties
│
├─.settings
│ org.eclipse.jdt.core.prefs
│
├─assets
├─bin
│ │ AndroidManifest.xml
│ │ classes.dex
│ │ HelloAndroid.apk
│ │ resources.ap_
│ │
│ ├─classes
│ │ └─com
│ │ └─example
│ │ └─helloandroid
│ │ BuildConfig.class
│ │ MainActivity.class
│ │ R$attr.class
│ │ R$dimen.class
│ │ R$drawable.class
│ │ R$id.class
│ │ R$layout.class
│ │ R$menu.class
│ │ R$string.class
│ │ R$style.class
│ │ R.class
│ │
│ └─res
│ ├─drawable-hdpi
│ │ ic_action_search.png
│ │ ic_launcher.png
│ │
│ ├─drawable-ldpi
│ │ ic_launcher.png
│ │
│ ├─drawable-mdpi
│ │ ic_action_search.png
│ │ ic_launcher.png
│ │
│ └─drawable-xhdpi
│ ic_action_search.png
│ ic_launcher.png
│
├─gen
│ └─com
│ └─example
│ └─helloandroid
│ BuildConfig.java
│ R.java
│
├─libs
│ android-support-v4.jar
│
├─res
│ ├─drawable-hdpi
│ │ ic_action_search.png
│ │ ic_launcher.png
│ │
│ ├─drawable-ldpi
│ │ ic_launcher.png
│ │
│ ├─drawable-mdpi
│ │ ic_action_search.png
│ │ ic_launcher.png
│ │
│ ├─drawable-xhdpi
│ │ ic_action_search.png
│ │ ic_launcher.png
│ │
│ ├─layout
│ │ activity_main.xml
│ │
│ ├─menu
│ │ activity_main.xml
│ │
│ ├─values
│ │ dimens.xml
│ │ strings.xml
│ │ styles.xml
│ │
│ ├─values-large
│ │ dimens.xml
│ │
│ ├─values-v11
│ │ styles.xml
│ │
│ └─values-v14
│ styles.xml
│
└─src
└─com
└─example
└─helloandroid
MainActivity.java
-
src
directory: Contains all source files. -
R.java
file: Is automatically generated by the Android SDK integrated in Eclipse. You do not need to modify its contents. -
Android library: A set of Java libraries used by Android applications.
-
assets
directory: Stores mostly multimedia files and other files. -
res
directory: Stores preconfigured resource files such as drawable layouts used by applications. -
values
directory: Stores mostlystrings.xml
,colors.xml
, andarrays.xml
. -
AndroidManifest.xml
: Equivalent to an application configuration file. Contains the application’s name, activity, services, providers, receivers, permissions, and so on. -
drawable
directory: Stores mostly image resources used by applications. -
layout
directory: Stores mostly layout files used by applications. These layout files are XML files.
src
folder contains all the .java
files for a project; and a res
folder contains all the project resources, such as application icons (drawable), layout files, and constant values.AndroidManifest.xml
file, a must-have of every Android project, and the R.java
file in the gen
folder, which is included in other Java projects.AndroidManifest.xml
AndroidManifest.xml
file contains information about your app essential to the Android system, which the system must have before it can run any of the app’s code. This information includes activities, services, permissions, providers, and receivers used in the project. An example is shown in Figure 1-5.
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyMainActivity"
android:label="@string/title_activity_my_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
AndroidManifest.xml
file is a text file in XML format, with each attribute defined by a name = value
pair. For example, in Android, label = "@ string / title_activity_my_main", label
indicates the name of the Android application as activity_my_main
.<
) and end (/>
) tags:<Type Name [attribute set]> Content </ type name>
<Type Name Content />
The format [attribute set]
can be omitted; for example, the <intent-filter> ... </ intent-filter>
text segment corresponds to the activity content of the element, and <action... />
corresponds to the action
element.action
element is nested within the intent-filter
element, which illustrates certain aspects of the properties or settings of intent-filter
. Detailed information about XML is beyond the scope of this book, but many excellent XML books are available.intent-filter
describes the location and time when an activity is launched and creates an intent
object whenever an activity (or OS) is to execute an operation. The information carried by the intent
object can describe what you want to do, which data and type of data you want to process, and other information. Android compares the intent-filter
data exposed by each application and finds the most suitable activity to handle the data and operations specified by the caller.AndroidManifest.xml
file are listed in Table 1-2.AndroidManifest.xml
FileParameter | Description |
---|---|
Manifest
| Root node that contains all contents in the package. |
xmlns:android
| Contains the manifest of the namespace. xmlns:android=
http://schemas.android.com/apk/res/android
. Makes various standard properties usable in the file and provides data to most elements. |
package
| Package of manifest application. |
Application
| Contains the root node of the application-level component manifest in the package. This element can also contain some global and default properties for the application, such as label, icon, theme, and necessary permissions. One manifest may contain zero or one (no more than one) element. |
android:icon
| Icon of the application. |
android:label
| Name of the application. |
Activity
| Name of the initial page to load when users start the application. It is an important tool for user interaction. Most other pages are displayed when other activities are performed or manifested by other activity flags. Note: Each activity must have a corresponding <activity> flag whether it is used externally or in its own package. If an activity has no corresponding flag, you cannot operate it. In addition, to support a searching activity, an activity can contain one or several <intent-filter> elements to describe the operations it supports. |
android:name
| Default activity launched by the application. |
intent-filter
| Is formed by manifesting the intent value supported by a designated component. In addition to specifying different types of values, intent-filter can specify properties for describing a unique label, icon, or other information required by an operation. |
Action
| Intent action supported by a component. |
Category
| Intent category supported by a component. The default activity launched by the application is designated here. |
uses-sdk
| Related to the SDK version used by the application. |
R.java
R.java
file is generated automatically when a project is created. It is a read-only file and cannot be modified. R.java is an index file defining all resources of the project. For example:/* AUTO-GENERATED FILE. DO NOT MODIFY.
... ...
*/
package com.example.helloandroid;
public final class R {
public static final class attr {
}
public static final class dimen {
public static final int padding_large=0x7f040002;
public static final int padding_medium=0x7f040001;
public static final int padding_small=0x7f040000;
}
public static final class drawable {
public static final int ic_action_search=0x7f020000;
public static final int ic_launcher=0x7f020001;
}
public static final class id {
public static final int menu_settings=0x7f080000;
}
public static final class layout {
public static final int activity_my_main=0x7f030000;
}
public static final class menu {
public static final int activity_my_main=0x7f070000;
}
public static final class string {
public static final int app_name=0x7f050000;
public static final int hello_world=0x7f050001;
public static final int menu_settings=0x7f050002;
public static final int title_activity_my_main=0x7f050003;
}
public static final class style {
public static final int AppTheme=0x7f060000;
}
}
res
folder, which proves that the R.java
file stores the index of all resources of the project. With this file, it is more convenient to use resources in applications and identify the resources required. Because this file does not allow manual editing, you only need to refresh the project when adding new resources to it. The R.java
file automatically generates the index of all resources.Definition File of Constants
values
subdirectory of the project contains a definition file for the strings, colors, and array constants; the string constant definitions are in the strings.xml
file. These constants are used by other files in the Android project.strings.xml
file. The Resources tab provides a structured view of the name-value
, and the strings.xml tab directly displays the contents of a text file format. The strings.xml
file of the HelloAndroid example is shown in Figure 1-6.
<resources>
<string name="app_name">HelloAndroid</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
Layout Files
View
class and its descendant classes and are stored in the android.widget
package.res\layout
directory. The file name in the generated application projects is specified in the next section: in this case, the source code file name corresponds to the [Layout Name]
key, so the file is named activity_main.xml
.
activity_main.xml
), you can see the corresponding contents of the XML-formatted text file, as shown in Figure 1-8.
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
-
<RelativeLayout>
: The layout configuration for the relative position. -
android:layout_width
: Customizes the screen width of the current view;match_parent
represents the parent container (in this case, the activity) match;fill_parent
fills the entire screen;wrap_content
, expressed as text fields, changes depending on the width or height of this view. -
android:layout_height
: Customizes the screen height occupied by the current view.
-
android:orientation
: Here means the layout is arranged horizontally. -
android:layout_weight
: Give a value for the importance assigned to multiple views of a linear layout. All views are given alayout_weight
value; the default is zero.
Source Code File
.java
source code file that contains the application basic runtime code for the project. It is located in the project folder under the src\com\example\XXX
directory (where XXX
is the project name). The file name of the generated application projects in this case is the source code file name that corresponds to the [Activity Name]
key, so the file is named MainActivity.java
.MainActivity.java
is as follows:package com.example.flashlight;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MyMainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_my_main, menu);
return true;
}
}
Component Structure of Applications
View
components) contain lists, grids, text boxes, buttons, and even an embedded web browser.-
Activity
-
Intent receiver
-
Service
-
Content provider
-
Intent and intent filters
Activity
View
components responding to events.Intent and Intent Filters
intent
. An intent
describes what the program does. The two most important parts of the data structure are the action and the data processed in accordance with established rules (data). Typical operations are MAIN
(activity entrance), VIEW
, PICK
, and EDIT
. Data to be used in the operation is presented using a Universal Resource Identifier (URI). For example, to view a person’s contact information, you need to create an intent
using the VIEW
operation, and the data is a pointer to the person’s URI.intent
is called an IntentFilter
. An intent
encapsulates a request as an object; IntentFilter
then describes what intentions an activity (or, say, an intent receiver, explained in a moment) can process. In the previous example, the activity that shows a person’s contact information uses an IntentFilter
, and it knows how to handle the data VIEW
operation applied to this person. The activity in the AndroidManifest.xml
file using IntentFilter
is usually accomplished by parsing the intent
activity switch. First, it uses the startActivity (myIntent)
function to start the new activity, next it systematically checks the IntentFilter
of all installed programs, and then it finds the activity that is the best match with the myIntent
corresponding to IntentFilter
. This new activity receives the message from intent
and then starts. The intent
-resolution process occurs in real time in the startActivity
called. This process has two advantages:
-
The activity emits only one
intent
request and can reuse the function of other components. -
The activity can always be replaced by an equivalent new activity of the
IntentFilter
.
Service
Mode | Start | End | Visit | Notes |
---|---|---|---|---|
Start/stop | Context.startService()
| Context.stopService()
| Even if the process of the startService call is ended, the service is still there until the process calls stopService() or the service causes its own demise (stopSelf() is called). | |
Bind/unbind | Context.bindService()
| Context.unbindService()
| Context.ServiceConnection()
| When calling bindService() , the process is dead; then the service it binds to must be ended. |
startService()
and other modes call bindService()
—then only when both the stopService
call and the unbindService
call occur will the service be terminated.-
If the service is the implementation process of the method
onCreate()
,onStart
, oronDestroy()
, then the main process becomes a foreground process to ensure that this code is not stopped. -
If the service has started, the value of its importance is lower than that of the visible process but above all invisible processes. Because only a few processes are visible to the user, as long as the memory is not particularly low, the service does not stop.
-
If multiple clients have bound to the service, as long as any one of the clients is visible to the user, that service is visible.
Broadcast Intent Receiver
IntentReceiver
. Intent receivers have no UI and use NotificationManager
to inform users that their event has happened. An intent receiver is declared in the AndroidManifest.xml
file but can also be declared using Context.registerReceiver()
. The program does not have to run continuously to wait for IntentReceiver
to be called. When an intent receiver is triggered, the system starts your program. Programs can also use Context.broadcastIntent()
to send their intent broadcast to other programs.AndroidManifest.xml
file. AndroidManifest.xml
contains the necessary configuration information, so the application is properly installed on the device. AndroidManifest.xml
also includes the necessary class names and the types of events that can be handled by the application, as well as the necessary permissions to run the application. For example, if an application needs to access the network—to, say, download a file—the manifest file must be explicitly listed in the license. Many applications may enable this particular license. This declarative security can help reduce the possibility of damage to equipment from malicious applications.Content Provider
Android Emulator
-
The general JVM is based on the stack-based virtual machine, but the DVM is a register-based virtual machine. The latter is better because applications can achieve maximum optimization based on the hardware, which is more in line with the characteristics of mobile devices.
-
The DVM can run multiple virtual machine instances simultaneously in limited memory, so that each DVM application executes as a separate Linux process. In the general JVM, all applications run in a shared JVM, and therefore individual applications are not running as separate processes. With each application running as a separate process, the DVM can be prevented from closing all programs in the event of the collapse of the virtual machine.
-
The DVM provides a less restrictive license platform than the general JVM. The DVM and JVM support different generic code. The DVM does not run standard Java bytecode, but rather Dalvik executable format (
.dex
). Java code compilation of Android applications actually consists of two processes. The first step is to compile the Java source code into normal JVM executable code, which uses the file-name suffix.class.
The second step is to compile the bytecode into Dalvik execution code, which uses the file-name suffix.dex
. The first step compiles the source code files under thesrc
subdirectory in the project directory into.class
files in thebin\class
directory; and the second step moves the files from thebin\class
subdirectory toclasses.dex
files in thebin
directory. The compilation process is integrated into the Eclipse build process; however, you can also use the command line to compile manually.
Introducing Android Runtime (ART)
Dalvik | ART | |
---|---|---|
Application
| APK package with DEX class file | Same as Dalvik |
Compile Type
| Dynamic compilation (JIT) | Ahead-of-time compilation (AOT) |
Functionality
| Stable and went through extensive QA | Basic functionality and stability |
Installation Time
| Faster | Slower due to compilation |
App Launch Time
| Mostly slower due to JIT compilation and interpretation | Mostly faster due to AOT compilation |
Storage Footprint
| Smaller | Larger, with precompiled binary |
Memory Footprint
| Larger due to JIT code cache | Smaller |
Summary
Context
class, intent, and the relationship between applications and activities.