Android Software Developmentتطوير برمجيات أندرويد
مقدمة الفصل
ماذا ستتعلم في هذا الفصل
- Overview
- The Tools
- The Architecture of Android Applications
- The Execution Model for Android Applications
- View Groups
- Simple Views
- An Example Application
أقسام الفصل
- 1Overview and toolsنظرة عامة والأدوات
- 2Architecture: activities, view files and intentsالبنية: الأنشطة وملفات العرض والنوايا
- 3Execution model and the activity life cycleنموذج التنفيذ ودورة حياة النشاط
- 4View groups and layoutsمجموعات العرض والتخطيطات
- 5Simple views and event handlersالعروض البسيطة ومعالجات الأحداث
- 6Creating the Hello Android exampleإنشاء مثال Hello Android
تلميح: استخدم أسهم لوحة المفاتيح للتنقل بين الشرائح.
Overview and toolsنظرة عامة والأدوات
Smart phone software platforms (slide 4)
- Apple's iOS
- Google's Android
- Microsoft's Windows Phone
An open-source operating system developed for smart phones. Because the software is written in Java and executed with interpreters, similar to but not the same as the JVM, Android is relatively easy to port to any hardware platform.
The tools (slide 5)
- Several powerful IDEs are available, with associated tools.
- The first required tool is the Java Development Kit (JDK) — just the Java Runtime Environment (JRE) is not sufficient.
- The other tools are bundled in the Android Software Development Kit (SDK).
Architecture: servlets, activities and view filesالأنشطة وملفات العرض
Architecture: activities, view files and intents
- Java servlets act as slaves to the servlet engine, which instantiates the servlet class and calls one of its methods, usually doGet or doPost (slide 6).
Related to servlets. A class instance that has an associated view markup file to support user interactions; it manages one page of display. Written in Java; an application includes one or more activities.
Corresponds to the forms in a Web application; its components are similar to form components. Each activity has an associated view file.
Intents and implementationالنوايا والتنفيذ
Architecture: activities, view files and intents
A means of communications, both within applications and among applications; an instance of a Java class. Intents are used to start and stop activities; control moves among activities using intents, and data is also passed among activities and applications with intents.
- Android applications are built as projects — single deployable systems, the simplest of which includes one or a few activities.
- The Java code is compiled to an intermediate form and interpreted, but not with a standard JVM.
The execution modelنموذج التنفيذ
Execution model and the activity life cycle
- An application begins when the user starts it, and continues until the user starts another application or the operating system stops it because it needs its resources for higher-priority applications.
- Android controls execution with a collection of predefined callback methods that are implicitly called when certain events are raised by system or user actions.
- Activities are placed on a stack: the activity on top of the stack is the one in execution and displayed; a new application's initial activity is placed on top.
The life cycle of an Android activityدورة حياة النشاط
Execution model and the activity life cycle
Starting an activity (slides 13–15)
- onCreate
Sets the activity's interface (view), often initializes class-scope variables → created state. Called as soon as the user selects the application's icon.
- onStart
Starts the activity and displays its view → started state. Called as soon as onCreate finishes.
- onResume
Starts execution of the activity's code → resumed state, where the user interacts with the view. Called as soon as onStart finishes.
| State (Figure 14.1) | Visibility | Reached by |
|---|---|---|
| Created | لا تذكر الشرائح شيئًا هنا. | onCreate |
| Started | visible | onStart (after onCreate), or onRestart from Stopped |
| Resumed | visible | onResume (after onStart, or from Paused) |
| Paused | partially visible | onPause from Resumed |
| Stopped | hidden | onStop from Paused |
| Destroyed | لا تذكر الشرائح شيئًا هنا. | onDestroy from Stopped |
- Created and started are transitional states.
- Paused → resumed when onResume is called; stopped → started when onRestart is called.
سؤال سريع
Execution model and the activity life cycle
View groups and layout managersمجموعات العرض والتخطيطات
View groups and layouts
- Android applications are built as Model-View-Controller (MVC) applications, so markup elements are called views — all extensions of the View class.
- The user interface is typically a collection of views similar to an HTML form.
- Layout managers (layouts) are derived from the ViewGroup class.
| Layout manager | How it arranges its views (slide 18) |
|---|---|
| LinearLayout | In either a vertical column or a horizontal row |
| FrameLayout | Pins its contained views around its frame |
| RelativeLayout | Allows the designer to position views relative to other views in the layout |
| GridLayout | In a grid |
Layout markupترميز التخطيط
View groups and layouts
- The markup is an XML language defined for the purpose; the tag set uses the prefix android.
- The first attribute of a layout element is always xmlns: the namespace http://schemas.android.com/apk/res/android.
TextView and EditTextTextView و EditText
Simple views and event handlers
Defines fixed text, often used for labeling other views. Usually four attributes: id, layout_width, layout_height, text.
A subclass of TextView configured to allow its elements to be edited; similar to HTML textboxes and textarea, but more powerful. The hint attribute places initial text in it.
ماذا تعني كل علامة في "@+id/the_name"؟
- @
The XML parser should parse and expand the rest of the string and identify it as a **resource id**.
- +
This is a **new resource name** that must be created and added to the resources, placed in the Android-generated file **R.java**. Java code refers to the view with **findViewById**.
Buttons, events and handlersالأزرار والأحداث
Simple views and event handlers
- Android button elements are similar to those of HTML; the label is specified with the text attribute.
- For android:text = "@string/button_send", res/values/strings.xml has <string name = "button_send" > Send </string>.
- The event for a button click is Click. A handler can be registered in markup or in code; in markup, the handler method's name is assigned to the onClick attribute: android:onClick = "the_handler".
Creating your first Android appأول تطبيق أندرويد
Creating the Hello Android example
- 1
Set up your development environment: install Android Studio with Android SDK (Android libraries); install JDK if it is not in your computer; create AVD (Android virtual device).
- 2
Create a new Android project in Eclipse: File → New → Project; select "Android Project"; fill in project details (Android version, directory name, name that appears on device, Java package, class to automatically create).
- 3
Run it in the emulator: Run → Run; select "Android Application". The emulator may take a few minutes to start; you don't need to restart it for a new version of your application.
- The project tree (slide 31) shows string constants, source code, configuration, auto-generated code and the UI layout.
- Slide 33: the emulator shows the title "Hello Android" and the text "Hello World, HelloAndroid!".
The Hello Android filesملفات Hello Android
Creating the Hello Android example
public class HelloAndroid extends Activity; onCreate(Bundle savedInstanceState) calls super.onCreate(savedInstanceState) and setContentView(R.layout.main).
A vertical LinearLayout (fill_parent width and height) containing a TextView with android:text="@string/hello".
<string name="hello">Hello World, HelloAndroid!</string> and <string name="app_name">Hello, Android</string>.
Declares the application (icon, label @string/app_name) and the activity ".HelloAndroid" with an intent-filter: action android.intent.action.MAIN, category android.intent.category.LAUNCHER.
الخلاصة
أنهيت الدرس
الخطوة التالية: ثبّت المعلومات بالبطاقات، ثم اختبر نفسك بالأسئلة.
ما راجعته في هذا الدرس
- Overview and toolsنظرة عامة والأدوات
- Architecture: activities, view files and intentsالبنية: الأنشطة وملفات العرض والنوايا
- Execution model and the activity life cycleنموذج التنفيذ ودورة حياة النشاط
- View groups and layoutsمجموعات العرض والتخطيطات
- Simple views and event handlersالعروض البسيطة ومعالجات الأحداث
- Creating the Hello Android exampleإنشاء مثال Hello Android