Android Software Developmentتطوير برمجيات أندرويد

الشريحة 1 من 14

مقدمة الفصل

تفاصيل المصدرالفصل 10 · الشريحة ⁦3⁩
المصدر
الفصل 10 · الشريحة ⁦3⁩

Overview and toolsنظرة عامة والأدوات

Smart phone software platforms (slide 4)

  • Apple's iOS
  • Google's Android
  • Microsoft's Windows Phone
Android
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).

A student's computer has only the Java Runtime Environment (JRE). Is this enough for Android development?

تفاصيل المصدرالفصل 10 · الشرائح ⁦4–5⁩
المصدر
الفصل 10 · الشرائح ⁦4–5⁩

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).
Activityالنشاط
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.
View fileملف العرض
Corresponds to the forms in a Web application; its components are similar to form components. Each activity has an associated view file.

Which definition of an activity is correct?

تفاصيل المصدرالفصل 10 · الشرائح ⁦6–8⁩
المصدر
الفصل 10 · الشرائح ⁦6–8⁩

Intents and implementationالنوايا والتنفيذ

Architecture: activities, view files and intents

Intent
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.

An app must open a second screen (activity) and send it the user's name. Which part of an Android application is used for this?

تفاصيل المصدرالفصل 10 · الشرائح ⁦9–10⁩
المصدر
الفصل 10 · الشرائح ⁦9–10⁩

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 activities of an application are placed on a stack. Which activity is currently executing and displayed?

تفاصيل المصدرالفصل 10 · الشرائح ⁦11–12⁩
المصدر
الفصل 10 · الشرائح ⁦11–12⁩

The life cycle of an Android activityدورة حياة النشاط

Execution model and the activity life cycle

Starting an activity (slides 13–15)

  1. 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.

  2. onStart

    Starts the activity and displays its view → started state. Called as soon as onCreate finishes.

  3. onResume

    Starts execution of the activity's code → resumed state, where the user interacts with the view. Called as soon as onStart finishes.

Figure 14.1: a simplified graph of the activity life cycle (slide 16)
State (Figure 14.1)VisibilityReached by
Createdلا تذكر الشرائح شيئًا هنا.onCreate
StartedvisibleonStart (after onCreate), or onRestart from Stopped
ResumedvisibleonResume (after onStart, or from Paused)
Pausedpartially visibleonPause from Resumed
StoppedhiddenonStop from Paused
Destroyedلا تذكر الشرائح شيئًا هنا.onDestroy from Stopped
  • Created and started are transitional states.
  • Paused → resumed when onResume is called; stopped → started when onRestart is called.

In which order are the callbacks called when the user starts an application by selecting its icon?

تفاصيل المصدرالفصل 10 · الشرائح ⁦13–16⁩
المصدر
الفصل 10 · الشرائح ⁦13–16⁩

سؤال سريع

Execution model and the activity life cycle

An activity is in the stopped (hidden) state. Which callback moves it back to the started state?

تفاصيل المصدرالفصل 10 · الشرائح ⁦15–16⁩
المصدر
الفصل 10 · الشرائح ⁦15–16⁩

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.
The four kinds of layout managers in the Android SDK (slide 18)
Layout managerHow it arranges its views (slide 18)
LinearLayoutIn either a vertical column or a horizontal row
FrameLayoutPins its contained views around its frame
RelativeLayoutAllows the designer to position views relative to other views in the layout
GridLayoutIn a grid

A designer wants to place a button "below the text box and aligned to its left edge". Which layout manager allows positioning views relative to other views?

تفاصيل المصدرالفصل 10 · الشرائح ⁦17–18⁩
المصدر
الفصل 10 · الشرائح ⁦17–18⁩

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.

من الشرائح

  • <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > … </LinearLayout>
تفاصيل المصدرالفصل 10 · الشرائح ⁦19–20⁩
المصدر
الفصل 10 · الشرائح ⁦19–20⁩

TextView and EditTextTextView و EditText

Simple views and event handlers

TextView
Defines fixed text, often used for labeling other views. Usually four attributes: id, layout_width, layout_height, text.
EditText
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"؟

  1. @
  2. +

من الشرائح

  • <TextView android:id = "@+id/label1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "The temperature is: " />
  • Android devices normally do not display any keyboard; tapping in the text field displays a standard keyboard and places the cursor there (Figure 14.2).

In android:id = "@+id/label1", what does the + indicate?

تفاصيل المصدرالفصل 10 · الشرائح ⁦21–24⁩
المصدر
الفصل 10 · الشرائح ⁦21–24⁩

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".

Using the markup approach, how is a click handler named the_handler attached to a Button?

تفاصيل المصدرالفصل 10 · الشرائح ⁦25–26⁩
المصدر
الفصل 10 · الشرائح ⁦25–26⁩

Creating your first Android appأول تطبيق أندرويد

Creating the Hello Android example

  1. 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. 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. 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!".
تفاصيل المصدرالفصل 10 · الشرائح ⁦27–33⁩
المصدر
الفصل 10 · الشرائح ⁦27–33⁩

The Hello Android filesملفات Hello Android

Creating the Hello Android example

HelloAndroid.java
public class HelloAndroid extends Activity; onCreate(Bundle savedInstanceState) calls super.onCreate(savedInstanceState) and setContentView(R.layout.main).
main.xml
A vertical LinearLayout (fill_parent width and height) containing a TextView with android:text="@string/hello".
strings.xml
<string name="hello">Hello World, HelloAndroid!</string> and <string name="app_name">Hello, Android</string>.
AndroidManifest.xml
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.
تفاصيل المصدرالفصل 10 · الشرائح ⁦34–37⁩
المصدر
الفصل 10 · الشرائح ⁦34–37⁩

الخلاصة

تفاصيل المصدر
المصدر
الفصل 10

خريطة الفصل