ANDROID SERVICES, BROADCAST RECEIVER, APPLICATION RESOURCES AND PROCESS

Similar documents
CS378 -Mobile Computing. Services and Broadcast Receivers

Services. Background operating component without a visual interface Running in the background indefinitely

Android Fundamentals - Part 1

Services are software components designed specifically to perform long background operations.

MOBILE APPLICATION DEVELOPMENT LECTURE 10 SERVICES IMRAN IHSAN ASSISTANT PROFESSOR

Lecture 2 Android SDK

Mobile Programming Practice Background processing AsynTask Service Broadcast receiver Lab #5

Mobile Application Development - Android

Software Practice 3 Today s lecture Today s Task

Software Practice 3 Before we start Today s lecture Today s Task Team organization

EMBEDDED SYSTEMS PROGRAMMING Android Services

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

App Development for Android. Prabhaker Matet

ANDROID APPS (NOW WITH JELLY BEANS!) Jordan Jozwiak November 11, 2012

Android HelloWorld - Example. Tushar B. Kute,

Android. Broadcasts Services Notifications

Services. Marco Ronchetti Università degli Studi di Trento

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL II)

Android System Development Day-4. Team Emertxe

Mobile Application Development Android

Android Application Development 101. Jason Chen Google I/O 2008

Mobile Computing. Introduction to Android

Services Broadcast Receivers Permissions

Android Services & Local IPC: The Command Processor Pattern (Part 1)

Security model. Marco Ronchetti Università degli Studi di Trento

Introduction to Android

Mobile Application Development Android

Getting started: Installing IDE and SDK. Marco Ronchetti Università degli Studi di Trento

Android App Development

application components

Mobile OS. Symbian. BlackBerry. ios. Window mobile. Android

Android Online Training

ANDROID SYLLABUS. Advanced Android

Syllabus- Java + Android. Java Fundamentals

ORACLE UNIVERSITY AUTHORISED EDUCATION PARTNER (WDP)

Android Essentials with Java

Application Fundamentals

Android. Mobile operating system developed by Google A complete stack. Based on the Linux kernel Open source under the Apache 2 license

COLLEGE OF ENGINEERING, NASHIK-4

Mobile Application Development Android

Android System Architecture. Android Application Fundamentals. Applications in Android. Apps in the Android OS. Program Model 8/31/2015

INTRODUCTION TO ANDROID

Services. service: A background task used by an app.

Android Services & Local IPC: Overview of Programming Bound Services

EMBEDDED SYSTEMS PROGRAMMING UI and Android

Questions and Answers. Q.1) Which of the following is the most ^aeuroeresource hungry ^aeuroepart of dealing with activities on android?

Programming with Android: Application Resources. Dipartimento di Scienze dell Informazione Università di Bologna

Embedded Systems Programming - PA8001

Android-Basics. Praktikum Mobile und Verteilte Systeme

CE881: Mobile & Social Application Programming

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

Real-Time Embedded Systems

The World of Android Development

Table of Content Android Tutorial... 2 Audience... 2 Prerequisites... 2 Copyright & Disclaimer Notice... 2 Overview... 7

Praktikum Mobile und Verteilte Systeme. Android-Basics. Prof. Dr. Claudia Linnhoff-Popien André Ebert, Sebastian Feld

CS378 -Mobile Computing. What's Next?

Android-Basics. Praktikum Mobile und Verteilte Systeme. Prof. Dr. Claudia Linnhoff-Popien André Ebert, Sebastian Feld

Android App Development. Muhammad Sharjeel COMSATS Institute of Information Technology, Lahore

CS378 -Mobile Computing. Anatomy of and Android App and the App Lifecycle

XML Tutorial. NOTE: This course is for basic concepts of XML in line with our existing Android Studio project.

Services. Marco Ronchetti Università degli Studi di Trento

Spring Lecture 5 Lecturer: Omid Jafarinezhad

CS 403X Mobile and Ubiquitous Computing Lecture 3: Introduction to Android Programming Emmanuel Agu

Programming Concepts and Skills. Creating an Android Project

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Android Application Development

SHWETANK KUMAR GUPTA Only For Education Purpose

Style, Themes, and Introduction to Material Design

Android Ecosystem and. Revised v4presenter. What s New

Multiple devices. Use wrap_content and match_parent Use RelativeLayout/ConstraintLayout Use configuration qualifiers

Android Architecture and Binder. Dhinakaran Pandiyan Saketh Paranjape

ANDROID DEVELOPMENT. Course Details

Minds-on: Android. Session 1

32. And this is an example on how to retrieve the messages received through NFC.

Programming in Android. Nick Bopp

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Android Software Development Kit (Part I)

Understanding Application

Introduction To Android

CS378 -Mobile Computing. Intents

Mobile Application Development

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Android framework. How to use it and extend it

Around Android. Essential Android features illustrated by a walk through a practical example

CS371m - Mobile Computing. Responsiveness

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Software Development & Education Center ANDROID. Application Development

App Development for Smart Devices. Lec #7: Windows Azure

Android Syllabus. Android. Android Overview and History How it all get started. Why Android is different.

CS 528 Mobile and Ubiquitous Computing Lecture 3b: Android Activity Lifecycle and Intents Emmanuel Agu

Building MyFirstApp Android Application Step by Step. Sang Shin Learn with Passion!

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Android Programming in Bluetooth Cochlea Group

Android Services. Victor Matos Cleveland State University. Services

Android User Interface

Transcription:

ANDROID SERVICES, BROADCAST RECEIVER, APPLICATION RESOURCES AND PROCESS 1 Instructor: Mazhar Hussain

Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background. 2

Services Started A service is "started" when an application component (such as an activity) starts it by calling startservice(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself. Bound A service is "bound" when an application component binds to it by calling bindservice(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed. 3

Services >A first-class component in Android that must be declared in the the manifest >Has no graphical user-interface >Used for long-running background tasks; typically networking, I/O, interaction with content providers, multimedia >There are two classes of Services; Service Started (local) Bound (remote) Used within the same app Across apps 4

Services To create a service, you must create a subclass of Service (or one of its existing subclasses). In your implementation, you need to override some callback methods that handle key aspects of the service lifecycle and provide a mechanism for components to bind to the service, if appropriate. The most important callback methods you should override are: onstartcommand() The system calls this method when another component, such as an activity, requests that the service be started, by calling startservice() Once this method executes, the service is started and can run in the background indefinitely. If you implement this, it is your responsibility to stop the service when its work is done, by callingstopself() or stopservice(). (If you only want to provide binding, you don't need to implement this method.) 6

Services onbind()the system calls this method when another component wants to bind with the service (such as to perform RPC), by calling bindservice(). In your implementation of this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder. You must always implement this method, but if you don't want to allow binding, then you should return null. oncreate()the system calls this method when the service is first created, to perform one-time setup procedures (before it calls either onstartcommand() or onbind()). If the service is already running, this method is not called. ondestroy()the system calls this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc. This is the last call the service receives. 7

Services If a component starts the service by calling startservice() (which results in a call to onstartcommand()), then the service remains running until it stops itself with stopself() or another component stops it by callingstopservice(). If a component calls bindservice() to create the service (and onstartcommand() is not called), then the service runs only as long as the component is bound to it. Once the service is unbound from all clients, the system destroys it. 8

Broadcast Receivers are meant to respond to an intent (usually one sent by a service or a system event), do something, and be done. An example here might be the user touches an Near Field Communication (NFC)-enabled phone to a tag, the system creates an intent for it, and a registered receiver handles it to change some settings (change volume, turn on bluetooth, etc). When an intent is broadcast via sendbroadcast, it will be sent to all receivers that have matching intent filters. *NFC, short for Near Field Communication, is a short range wireless RFID technology that makes use of interacting electromagnetic radio fields instead of the typical direct radio transmissions used by technologies such as Bluetooth. 9

Example : You want to perform some data analysis to find some patterns in your data Background Thread If all processing should happen while the user is in the same application and on the same Activity, a background thread (or an AsyncTask that manages a background thread) would be a good approach. Service If you want to allow the user to exit the application while the processing is being performed (and notify them of the results later), or allow them to progress through multiple activities in the same application while the processing is being performed, a Service would be a better approach. 10

Android App Resources 11

Applications Resource It takes more than just code to build a great app. Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. You should always externalize resources such as images and strings from your application code, so that you can maintain them independently. Externalizing your resources also allows you to provide alternative resources that support specific device configurations such as different languages or screen sizes, which becomes increasingly important as more Android-powered devices become available with different configurations. In order to provide compatibility with different configurations, you must organize resources in your project's res/ directory, using various sub-directories that group 12 resources by type and configuration.

For any type of resource, you can specify default and multiple alternative resources for your application: Default resources are those that should be used regardless of the device configuration or when there are no alternative resources that match the current configuration. Alternative resources are those that you've designed for use with a specific configuration. To specify that a group of resources are for a specific configuration, append an appropriate configuration qualifier to the directory name. 13

Providing Resources You should always externalize application resources such as images and strings from your code, so that you can maintain them independently. You should also provide alternative resources for specific device configurations, by grouping them in specially-named resource directories. At runtime, Android uses the appropriate resource based on the current configuration. For example, you might want to provide a different UI layout depending on the screen size or different strings depending on the language setting. 14

Grouping Resource Types 15

Grouping Resource Types 16

Accessing Resources Animation Resources Define pre-determined animations. Tween animations are saved in res/anim/ and accessed from the R.anim class. Frame animations are saved in res/drawable/ and accessed from the R.drawable class. Color State List Resource Define a color resources that changes based on the View state. Saved in res/color/ and accessed from the R.color class. Drawable Resources Define various graphics with bitmaps or XML. Saved in res/drawable/ and accessed from the R.drawable class. Layout Resource Define the layout for your application UI. Saved in res/layout/ and accessed from the R.layout class. Menu Rsource Define the contents of your application menus. Saved in res/menu/ and accessed from the R.menu class. 17

Accessing Resources String Resources Define strings, string arrays, and plurals (and include string formatting and styling). Saved in res/values/ and accessed from the R.string, R.array, and R.plurals classes. Style Resource Define the look and format for UI elements. Saved in res/values/ and accessed from the R.style class. More Resource Types Define values such as booleans, integers, dimensions, colors, and other arrays. Saved in res/values/ but each accessed from unique R sub-classes (such as R.bool, R.integer,R.dimen, etc.). 18

Android Processes 19

When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread). If an application component starts and there already exists a process for that application (because another component from the application exists), then the component is started within that process and uses the same thread of execution. However, you can arrange for different components in your application to run in separate processes, and you can create additional threads for any process. 20

Process lifecycle The Android system tries to maintain an application process for as long as possible, but eventually needs to remove old processes to reclaim memory for new or more important processes. To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy" based on the components running in the process and the state of those components. Processes with the lowest importance are eliminated first, then those with the next lowest importance, and so on, as necessary to recover system resources. There are five levels in the importance hierarchy. The following list presents the different types of processes in order of importance (the first process is most important and is killed last): 21

22

23

24

References 1. http://developer.android.com/guide 25

QUESTIONS/COMMENTS? 26