You are on page 1of 28

Universidad Aut onoma de Tamaulipas Facultad de Ingenier a y Ciencias

Programaci on en Android
M.C. Juan Jose Garza Saldana

M.C. Juan Jose Garza Saldana

Programaci on en Android

1/1

Contenido

Android Application Components fundamentals Application Components Activating components

Managing the Activity Lifecycle

Permissions

M.C. Juan Jose Garza Saldana

Programaci on en Android

2/1

Applications

Android applications are written in the Java The Android SDK tools compile the code and create an archive le with an .apk sux. All the code in a single .apk le .apk le is used to install the application

M.C. Juan Jose Garza Saldana

Programaci on en Android

3/1

Applications

Security Sandbox The Android operating system is a multi-user Linux system in which each application is form a dierent user. Each application has a user ID (assigned by system) System sets permision for all les in an app, only the user ID assigned access them Each process has its own VM By default, every application runs in its own Linux process.

M.C. Juan Jose Garza Saldana

Programaci on en Android

4/1

Applications

Security There are ways for an app to share data with other application and to access system services. Sharing the same Linux User ID. Request permission to access users contacts, SMS messages, SD card, camera, ...

M.C. Juan Jose Garza Saldana

Programaci on en Android

5/1

Four types of application components

Activities An activity represents a single screen with a user interface. For example, 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. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others.

M.C. Juan Jose Garza Saldana

Programaci on en Android

6/1

Four types of application components

Services A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a dierent application Or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.

M.C. Juan Jose Garza Saldana

Programaci on en Android

7/1

Four types of application components

Content providers A content provider manages a shared set of application data. You can store the data in the le system, an SQLite database, on the web, or any other persistent storage location your application can access. Through the content provider, other applications can query or even modify the data (if the content provider allows it). For example, the Android system provides a content provider that manages the users contact information. As such, any application with the proper permissions can query part of the content provider (such as ContactsContract.Data) to read and write information about a particular person.

M.C. Juan Jose Garza Saldana

Programaci on en Android

8/1

Four types of application components

Broadcast receivers Although broadcast receivers dont display a user interface, they may create a status bar notication to alert the user when a broadcast event occurs. More commonly, though, a broadcast receiver is just a gatewayto other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.

M.C. Juan Jose Garza Saldana

Programaci on en Android

9/1

Activating components

Three of the four component typesactivities, services, and broadcast receiversare activated by an asynchronous message called an intent. Intents bind individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your application or another. Content provider, is not activated by intents. Rather, it is activated when targeted by a request from a ContentResolver.

M.C. Juan Jose Garza Saldana

Programaci on en Android

10/1

The Manifest File

Application must declare all its components in this le Must be at the root of the application project directory

M.C. Juan Jose Garza Saldana

Programaci on en Android

11/1

The Manifest File

Identify any user permissions the application requires, such as Internet access or read-access to the users contacts. Declare the minimum API Level required by the application, based on which APIs the application uses. Declare hardware and software features used or required by the application, such as a camera, bluetooth services, or a multitouch screen. API libraries the application needs to be linked against (other than the Android framework APIs), such as the Google Maps library. More

M.C. Juan Jose Garza Saldana

Programaci on en Android

12/1

Starting an activity

Android system initiate code in an Activity instance (not in a main() function) There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity.

M.C. Juan Jose Garza Saldana

Programaci on en Android

13/1

Activity life cycle

M.C. Juan Jose Garza Saldana

Programaci on en Android

14/1

Starting an activity

Depending on the complexity of your activity, you probably dont need to implement all the lifecycle methods. Its important that you understand each one and implement those that ensure your app behaves the way users expect. Implementing your activity lifecycle methods properly ensures your app behaves well in several ways

M.C. Juan Jose Garza Saldana

Programaci on en Android

15/1

Starting an activity

Check list Does not crash if the user receives a phone call or switches to another app while using your app. Does not consume valuable system resources when the user is not actively using it. Does not lose the users progress if they leave your app and return to it at a later time. Does not crash or lose the users progress when the screen rotates between landscape and portrait orientation.

M.C. Juan Jose Garza Saldana

Programaci on en Android

16/1

Simplied life cycle

M.C. Juan Jose Garza Saldana

Programaci on en Android

17/1

Main Activity

Dene which activity to use as the main activity in the Android manifest le, AndroidManifest.xml

M.C. Juan Jose Garza Saldana

Programaci on en Android

18/1

Create a New Instance

M.C. Juan Jose Garza Saldana

Programaci on en Android

19/1

Destroy the Activity

Check list The system calls this method on your activity as the nal signal that your activity instance is being completely removed from the system memory. Most apps dont need to implement this method because local class references are destroyed with the activity and your activity should perform most cleanup

M.C. Juan Jose Garza Saldana

Programaci on en Android

20/1

Pausing and Resuming an Activity

M.C. Juan Jose Garza Saldana

Programaci on en Android

21/1

Pause Your Activity

Technically means your activity is still partially visible Often is an indication that the user is leaving the activity and it will soon enter the Stopped state Uses:
Stop animations or other ongoing actions that could consume CPU. Commit unsaved changes (such as a draft email). Release system resources, that may aect battery life while your activity is paused and the user does not need them.

M.C. Juan Jose Garza Saldana

Programaci on en Android

22/1

Resume Your Activity

When the user resumes your activity from the Paused state, the system calls the onResume() method. The system calls this method every time your activity comes into the foreground, including when its created for the rst time.

M.C. Juan Jose Garza Saldana

Programaci on en Android

23/1

Stopping and Restarting an Activity

Scenarios in which your activity is stopped and restarted The user opens the Recent Apps window and switches from your app to another app. The activity in your app thats currently in the foreground is stopped. If the user returns to your app from the Home screen launcher icon or the Recent Apps window, the activity restarts. The user performs an action in your app that starts a new activity. The current activity is stopped when the second activity is created. If the user then presses the Back button, the rst activity is restarted. The user receives a phone call while using your app on his or her phone.

M.C. Juan Jose Garza Saldana

Programaci on en Android

24/1

Stopping and Restarting an Activity

M.C. Juan Jose Garza Saldana

Programaci on en Android

25/1

Recreating an Activity

M.C. Juan Jose Garza Saldana

Programaci on en Android

26/1

Permissions

Using Permissions A basic Android application has no permissions associated with it by default. Meaning it can not do anything that would adversely impact the user experience or any data on the device Must include in your AndroidManifest.xml one or more <uses-permission> tags declaring the permissions that your application needs

M.C. Juan Jose Garza Saldana

Programaci on en Android

27/1

Permissions

Using Permissions At application install time, permissions requested by the application are granted to it by the package installer. No checks with the user are done while an application is running (It can run or not because of permissions). http://developer.android.com/guide/topics/security/permissions.html

M.C. Juan Jose Garza Saldana

Programaci on en Android

28/1

You might also like