-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKB_basics
146 lines (118 loc) · 10.6 KB
/
KB_basics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
Layouts only define the appearance of the app. You define what the app does using one or more activities. An activity is a special Java class that decides which layout to use and tells the app how to respond to the user. As an example, if a layout includes a button, you need to write Java code in the activity to define what the button should do when you press it.
In addition to activities and layouts, Android apps often need extra resources such as image files and application data. You can add any extra files you need to the app.
Android apps are really just a bunch of files in particular directories. When you build your app, all of these files get bundled together, giving you an app you can run on your device.
The Android platform is made up of a number of different components. It includes core applications such as Contacts, a set of APIs to help you control what your app looks like and how it behaves, and a whole load of supporting files and libraries.
The Android platform dissected:
Linux Kernel:
Android relies on the kernel for drivers, and also core services such as security and memory management.
1. Display Driver
2. Camera Driver
3. Flash Memory Driver
4. Binder (IPC) Driver
5. Keypad Driver
6. WiFi Driver
7. Audio Drivers
8. Power Management
Libraries:
Underneath the application framework lies a set of C and C++ libraries. These libraries get exposed to you through the framework APIs.
1. Surface Manager
2. Media Framework
3. SQLite
4. OpenGL | ES
5. FreeType
6. WebKit
7. SGL
8. SSL
9. libc
Android Runtime: Core Libraries
The Android runtime comes with a set of core libraries that implement most of the Java programming language. Each Android app runs in its own process.
Application Framework
When you build apps, you have access to the same APIs used by the core applications. You use these APIs to control what your app looks like and how it behaves.
1. Activity Manager
2. Window Manager
3. Content Providers
4. View System
5. Package Manager
6. Telephony Manager
7. Resource Manager
8. Location Manager
9. Notification Manager
Applications
Android comes with a set of core applications such as Home, Contacts, Phone, Calendar, and a browser
Android SDK (Android Software Development Kit)
The Android Software Development Kit contains the libraries and tools you need to develop Android apps.
1. SDK platform: There’s one of these for each version of Android.
2. SDK Tools: Tools for debugging and testing, plus other useful utilities. The SDK also features a set of platform dependent tools.
3. Documentation: So you can access the latest API documentation offline.
4. Android support: Extra APIs that aren’t available in the standard platform.
5. and so on
Android Studio
Android Studio is a version of IDEA that includes a version of the Android SDK and extra GUI tools to help you with your app development.
In addition to providing you with an editor and access to the tools and libraries in the Android SDK, Android Studio gives you templates you can use to help you create new apps and classes, and it makes it easy to do things such as package your apps and run them.
Android Versions
Android versions have a version number and a codename. The version number gives the precise version of Android (e.g., 7.0), while the codename is a more generic “friendly” name that may cover several versions of Android (e.g., Nougat). The API level refers to the version of the APIs used by applications. As an example, the equivalent API level for Android version 7.1.1 is 25.
Version codename API level
4.1~ 4.3 Jelly Bean 16 ~ 18
4.4 KitKat 19 ~ 20
5.0~5.1 Lollipop 21 ~ 22
6.0 Marshmallow 23
When you develop Android apps, you really need to consider which versions of Android you want your app to be compatible with. If you specify that your app is only compatible with the very latest version of the SDK, you might find that it can’t be run on many devices. You can find out the percentage of devices running particular versions here: https://developer.android.com/ about/dashboards/index.html.
Every Android app is a collection of screens, and each screen is composed of an activity and a layout.
Let’s look in more detail at how activities and layouts work together to create a user interface:
1. The device launches your app and creates an activity object.
2. The activity object specifies a layout.
3. The activity tells Android to display the layout onscreen.
4. The user interacts with the layout that’s displayed on the device.
5. The activity responds to these interactions by running application code.
6. The activity updates the display...
7. ...which the user sees on the device.
# Create the second activity and layout
To create the new activity, switch to the Project view of Android Studio’s explorer, click on the com.xxx.xxxx package in the app/src/main/java folder, choose File → New → Activity, and choose the option for Empty Activity. You will be presented with a new screen where you can choose options for your new activity.
Every time you create a new activity and layout, you need to name them
# Pass text to a second activity
So far we’ve coded CreateMessageActivity to start ReceiveMessageActivity when the Send Message button is pressed. Next, we’ll get CreateMessageActivity to pass text to ReceiveMessageActivity so that ReceiveMessageActivity can display it.
In order to accomplish this, we’ll do three things:
1 Tweak the layout activity_receive_message.xml so that it can display the text. At the moment it’s simply the default layout the wizard gave us.
2 Update CreateMessageActivity.java so that it gets the text the user inputs, and then adds the text to the intent before it sends it.
3 Update ReceiveMessageActivity.java so that it displays the text sent in the intent.
# How Android apps work
As you’ve seen, all Android apps are composed of one or more activities, along with other components such as layouts. Each activity is a single defined focused thing the user can do. As an example, apps such as Gmail, Google+, Facebook, and Twitter all have activities that enable you to send messages, even though they may achieve this in different ways.
Intents can start activities in other apps
You’ve already seen how you can use an intent to start a second activity within the same app. The first activity passes an intent to Android, Android checks it, and then Android tells the second activity to start.
The same principle applies to activities in other apps. You get an activity in your app to pass an intent to Android, Android checks it, and then Android tells the second activity to start even though it’s in another app.
Intent intent= new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,messageText);
startActivity(intent);
1. When the onSendMessage() method is called, an intent gets created. The startActivity() method passes this intent to Android.
2. Android sees that the intent can only be passed to activities able to handle ACTION_SEND and text/plain data. Android checks all the activities on the user’s device, looking for ones that are able to receive the intent. If no actions are able to handle the intent, an ActivityNotFoundException is thrown.
3. If just one activity is able to receive the intent, Android tells that activity to start and passes it the intent.
4. If more than one activity is able to receive the intent, Android displays an activity chooser dialog and asks the user which one to use.
5. When the user chooses the activity she wants to use, Android tells the activity to start and passes it the intent.
The intent filter tells Android which activities can handle which actions
When Android is given an intent, it has to figure out which activity, or activities, can handle it. This process is known as intent resolution.
* When you use an explicit intent, intent resolution is straightforward. The intent explicitly says which component the intent is directed at, so Android has clear instructions about what to do.
* When you use an implicit intent, Android uses the information in the intent to figure out which components are able to receive it. It does this by checking the intent filters in every app’s copy of AndroidManifest.xml. An intent filter specifies what types of intent each component can receive.
As an example, here’s the entry for an activity that can handle an action of ACTION_SEND. The activity is able to accept data with MIME types of text/plain or image:
<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="image/*"/>
</intent-filter>
</activity>
The intent filter also specifies a category. The category supplies extra information about the activity such as whether it can be started by a web browser, or whether it’s the main entry point of the app. An intent filter must include a category of android.intent.category. DEFAULT if it’s to receive implicit intents. If an activity has no intent filter, or it doesn’t include a category name of android.intent. category.DEFAULT, it means that the activity can’t be started with an implicit intent. It can only be started with an explicit intent using the full name (including the package) of the component.
# How Android uses the intent filter
When you use an implicit intent, Android compares the information given in the intent with the information given in the intent filters specified in every app’s AndroidManifest.xml file.
1. Android first considers intent filters that include a category of android. intent.category.DEFAULT:
2. Intent filters without this category will be omitted, as they can’t receive implicit intents.
3. Android then matches intents to intent filters by comparing the action and MIME type contained in the intent with those of the intent filters.
As an example, if an intent specifies an action of Intent.ACTION_SEND using:
Intent intent = new Intent(Intent.ACTION_SEND);
Android will only consider activities that specify an intent filter with an action of android.intent.action.SEND like this:
<intent-filter>
<action android:name="android.intent.action.SEND"/> ...
</intent-filter>
Similarly, for the intent MIME type. If the MIME type is left out of the intent, Android tries to infer the type based on the data the intent contains.
4. Once Android has finished comparing the intent to the component’s intent filters, it sees how many matches it finds. If Android finds a single match, it starts the component (in our case, the activity) and passes it the intent. If it finds multiple matches, it asks the user to pick one.