Professional Documents
Culture Documents
Introducere: Programare Pe Platforma Android
Introducere: Programare Pe Platforma Android
Programare pe platforma
Android
Sistemul de operare Android
● Bazat pe Linux
● Destinat in special dispozitivelor mobile cu
touchscreen
● Android Inc. → Google → Open Handset
Alliance (Google, HTC, Sony, Dell, Intel, Motorola, Samsung, LG, etc.)
● 2008 → Android 5.1 Lollipop (apr, 2015)
● Programare: C, C++, Java
● http://developer.android.com, Android Studio
● “Competitori”: iOS, Windows, Symbian
Aplicatii Java
● Distribuite sub forma: .apk (application package file)
● Java bytecode → Dalvik 'dex-code'
● Executia
– Dalvik virtual machine, JIT
– ART (Android Runtime), ahead-of-time (AOT)
compilation (de la v5.0)
● Aplicatie - Proces Linux - ID unic
● Proces - Masina virtuala - Izolare
● Permisiuni - trebuie acordate de utilizator la
instalarea aplicatiei
Structura unei aplicații
src/
res/
drawable/
layout/
values/
gen/
R.java
AndroidManifest.xml
<manifest ... >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
</manifest>
Componentele unei aplicații
● Activity: a single screen with a user interface
An email application might have one activity that shows a list of new emails, another
activity to compose an email, and another activity for reading emails
● Service: a component that runs in the
background; does not provide a user interface.
A service might play music in the background while the user is in a different
application, or it might fetch data over the network withoutblocking user interaction.
● Content Provider (file system, SQLite, ...) \\
A content provider manages a shared set of application data. You can store the data
in the file system, an SQLite database, on the web, or any other persistent storage
location your application can access
● Broadcast Receiver: responds to system-wide
broadcast announcements.
For example, a broadcast announcing that the screen has turned off, the battery is
low, or a picture was captured.
Activarea componentelor
● Intent: mesaj asincron ce leaga doua
componente la runtime.
● An intent is an abstract description of an operation to be
performed. It can be used with startActivity to launch an Activity,
broadcastIntent to send it to any interested BroadcastReceiver
components, and startService(Intent) or bindService(Intent,
ServiceConnection, int) to communicate with a background
Service.
● An Intent provides a facility for performing late runtime binding
between the code in different applications. Its most significant
use is in the launching of activities, where it can be thought of
as the glue between activities. It is basically a passive data
structure holding an abstract description of an action to be
performed.
Fisierul manifest
● Componente: <activity>, <service>, <receiver>,
<provider>
● Capabilitatile componentelor:<intent-filter>
● Necesitatile aplicatiei
– Dimensiune ecran: small, normal, large, extra large
– Densitate ecran (dpi): low, medium, high, extra high density
– Modul de interactiune: hardware keyboard, trackball, five-
way navigation pad, etc.
– Facilitați: camera, light sensor, bluetooth, etc.
– Versiunea platformei: minimum API-level
● ...
Hello World
Componente
● MainActivity.java
● DisplayMessageActivity.java
● res/layout/activity_main.xml
● res/layout/activity_display_message.xml
● res/values/strings.xml
● AndroidManifest.xml
● gen/R.java
activity_main.xml
<LinearLayout ...>
<EditText
android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
strings.xml
<resources>
<string name="app_name">HelloWorld</string>
<string name="action_settings">Settings</string>
<string name="button_send">Send</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">
My Message
</string>
</resources>
MainActivity.java
package com.example.helloworld;
import ...;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
DisplayMessageActivity.java
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
R.java
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.example.helloworld;