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

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

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

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

Minds-on: Android. Session 2

States of Activities. Active Pause Stop Inactive

Lab 1: Getting Started With Android Programming

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

Android Activities. Akhilesh Tyagi

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L

Android Basics. Android UI Architecture. Android UI 1

Activities and Fragments

UNDERSTANDING ACTIVITIES

Android Fundamentals - Part 1

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

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input

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

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

Android Application Development

Activities. Repo:

Overview of Activities

LECTURE NOTES OF APPLICATION ACTIVITIES

Mobila applikationer och trådlösa nät, HI1033, HT2013

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

Application Fundamentals

application components

Fragments. Lecture 11

Lifecycle-Aware Components Live Data ViewModel Room Library

Mobila applikationer och trådlösa nät, HI1033, HT2012

Understanding Application

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

Vienos veiklos būsena. Theory

CS 4518 Mobile and Ubiquitous Computing Lecture 4: Data-Driven Views, Android Components & Android Activity Lifecycle Emmanuel Agu

UI Fragment.

EMBEDDED SYSTEMS PROGRAMMING Application Basics

COLLEGE OF ENGINEERING, NASHIK-4

University of Babylon - College of IT SW Dep. - Android Assist. Lect. Wadhah R. Baiee Activities

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Mobile User Interfaces

CMSC436: Fall 2013 Week 3 Lab

Introduction to Android

Android Ecosystem and. Revised v4presenter. What s New

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

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

Application s Life Cycle

INTRODUCTION TO ANDROID

TextView Control. EditText Control. TextView Attributes. android:id - This is the ID which uniquely identifies the control.

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Xin Pan. CSCI Fall

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

CS 528 Mobile and Ubiquitous Computing Lecture 3: Android UI, WebView, Android Activity Lifecycle Emmanuel Agu

CS 4518 Mobile and Ubiquitous Computing Lecture 5: Rotating Device, Saving Data, Intents and Fragments Emmanuel Agu

An Overview of the Android Programming

Embedded Systems Programming - PA8001

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

shared objects monitors run() Runnable start()

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

Embedded Systems Programming - PA8001

Android for Ubiquitous Computing Researchers. Andrew Rice University of Cambridge 17-Sep-2011

Mobile Computing Fragments

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

Libraries are wri4en in C/C++ and compiled for the par>cular hardware.

Real-Time Embedded Systems

ELET4133: Embedded Systems. Topic 15 Sensors

<uses-permission android:name="android.permission.internet"/>

EMBEDDED SYSTEMS PROGRAMMING Android Services

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

MOBILE APPLICATION DEVELOPMENT LECTURE 10 SERVICES IMRAN IHSAN ASSISTANT PROFESSOR

CS378 -Mobile Computing. Services and Broadcast Receivers

Mobile Computing. Introduction to Android

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

Applied Cognitive Computing Fall 2016 Android Application + IBM Bluemix (Cloudant NoSQL DB)

Have a development environment in 256 or 255 Be familiar with the application lifecycle

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

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

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

ANDROID PROGRAMS DAY 3

1. Location Services. 1.1 GPS Location. 1. Create the Android application with the following attributes. Application Name: MyLocation

Android Tutorial: Part 3

LifeStreet Media Android Publisher SDK Integration Guide

Application s Life Cycle

PENGEMBANGAN APLIKASI PERANGKAT BERGERAK (MOBILE)

COSC 3P97 Mobile Computing

CS 528 Mobile and Ubiquitous Computing Lecture 4a: Fragments, Camera Emmanuel Agu

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Solving an Android Threading Problem

14.1 Overview of Android

Lecture 2 Android SDK

Spring Lecture 5 Lecturer: Omid Jafarinezhad

User Interface Design & Development

Programming with Android: Intents. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Android/Java Lightning Tutorial JULY 30, 2018

Fragments and the Maps API

Programming Android UI. J. Serrat Software Design December 2017

Developed and taught by well-known Contact author and developer. At public for details venues or onsite at your location.

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling

Computer Science E-76 Building Mobile Applications

Fragments. Lecture 10

GUI Widget. Lecture6

Transcription:

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I) Lecture 3: Android Life Cycle and Permission Android Lifecycle An activity begins its lifecycle when entering the oncreate() state If not interrupted or dismissed, the activity performs its job and finally terminates and releases its acquired resources when reaching the ondestroy() event Peter Lo 2 Entire Lifetime Visible Lifetime It happens between the first call to oncreate() through to a single final call to ondestroy(). An activity will do all setup of global state in oncreate(), and release all remaining resources in ondestroy(). Entire Lifetime For example, if it has a thread running in the background to download data from the network, it may create that thread in oncreate() and then stop the thread in ondestroy(). It happens between a call to onstart() until a corresponding call to onstop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. You can maintain resources that are needed to show the activity to the user between these two methods. For example, you can register a BroadcastReceiver in onstart() to monitor for changes that impact your UI, and unregister it in onstop() when the user no longer sees what you are displaying. The onstart() and onstop() methods can be called multiple times, as the activity becomes visible and hidden to the user. Visible Lifetime 3 4

Foreground Lifetime Life Cycle States Resumed It happens between a call to onresume() until a corresponding call to onpause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states Foreground Lifetime For example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered so the code in these methods should be fairly lightweight. It is active or running when it is in the foreground of the screen (at the top of the activity stack). This is the activity that has focus and its graphical interface is responsive to the user s interactions. 5 6 Life Cycle States Pause Life Cycle States Stopped Lost focus but is still visible to the user. Another activity lies on top of it and that new activity either is transparent or doesn't cover the full screen. A paused activity is alive, maintaining its state information and attachment to the window manager Paused activities can be killed by the system when available memory becomes extremely low. Completely obscured by another activity. Continues to retains all its state information. It is no longer visible to the user Its window is hidden and its life cycle could be terminated at any point by the system if the resources that it holds are needed elsewhere 7 8

Life Cycle Callbacks Activity Lifecycle All activities must implement oncreate() to do the initial setup when the object is first instantiated Activities should implement onpause() to commit data changes in anticipation to stop interacting with the user Applications do not need to implement each of the transition methods, however there are mandatory and recommended states to consider 9 If an activity in the foreground of the screen, it is active or running. If an activity has lost focus but is still visible, it is paused. A paused activity is completely alive, but can be killed by the system in extreme low memory situations. If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, but no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere. If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state. 10 10 Life Cycle Methods oncreate( ) Life Cycle Methods onpause( ) Called when the activity is first created. Most of your application s code is written here. Typically used to define listener s behavior, initialize data structures, wire-up UI view elements (buttons, text boxes, lists) with static Java controls, etc. It may receive a data Bundle object containing the activity's previous state (if any). Followed by onstart(). 11 Called when the system is about to transfer control to another activity. Gives you a chance to commit unsaved data, and stop work that may unnecessarily burden the system. The next activity waits until completion of this state. Followed either by onresume() if the activity returns back to the foreground, or by onstop() if it becomes invisible to the user. A paused activity could be killed by the system. 12

Killable States Killable Priority Activities on killable states can be terminated by the system when memory resources become critically low. Methods: onpause(), onstop() and ondestroy() are killable. onpause() is the only state that is guaranteed to be given a chance to complete before the process is killed. You should use onpause() to write any pending persistent data. If the Android system needs to terminate processes it follows the following priority system. Process Status Description Priority Foreground Visible An application in which the user is interacting with an activity, or which has an service which is bound to such an activity. Also if a service is executing one of its lifecycle methods or a broadcast receiver which runs its onreceive() method. User is not interacting with the activity, but the activity is still (partially) visible or the application has a service which is used by a inactive but visible activity. Service Application with a running service which does not qualify for 1 or 2. 3 Background Application with only stopped activities and without a service or executing receiver. Android keeps them in a Least Recent Used (LRU) list and if requires terminates the one which was least used. Empty Application without any active components. 5 1 2 4 13 14 Event Handling Approaches Handling Events by Using an Inner Class Events are a useful way to collect data about a user's interaction with interactive components of your app, like button presses or screen touch etc. There are several approaches for event handling: Handling Events by Using an Inner Class Handling Events by Having Main Activity Implement Listener Interface Handling Events by Specifying the Event Handler Method in Layout Use an inner class that implements the Listener Advantages Assuming that each class is applied to a single control only, same advantages as named inner classes, but shorter. This approach is widely used in Swing, SWT, AWT, and GWT Disadvantages If you applied the handler to more than one control, you would have to cut and paste the code for the handler. This approach should be applied for a single control only If the code for the handler is long, it makes the code harder to read by putting it inline. This approach is usually used only when handler code is short 15 16

Handling Events by Using an Inner Class Handling Events by Having Main Activity Implement Listener Interface @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // Attach the listener to the button Button button1 = (Button) findviewbyid(r.id.button1); button1.setonclicklistener( new OnClickListener() { ); public void onclick(view arg0) { This defines the class and instantiates it all in one fell swoop. This is very analogous to anonymous functions (closures) that are widely used in functional programming languages. 17 Have the main Activity implement the Listener interface. Put the handler method in the main Activity. Call setonclicklistener(this). Advantages Assuming that the app has only a single control of that Listener type, this is the shortest and simplest of the approaches Disadvantages Scales poorly to multiple controls unless they have completely identical behavior. If you assigned this as the handler for more than one control of the same Listener type, the onclick method would have to have cumbersome if statements to see which control was clicked This approach should be applied when your app has only a single control of that Listener type Cannot pass arguments to the Listener. Works poorly for multiple controls 18 Handling Events by Having Main Activity Implement Listener Interface Handling Events by Specifying the Event Handler Method in Layout public class MainActivity extends Activity implements OnClickListener { @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); Button button1 = (Button) findviewbyid(r.id.button1); button1.setonclicklistener(this); @Override public void onclick(view arg0) { 19 Put the handler method in the main Activity. Do not implement a Listener interface or call setonclicklistener. Have the layout file specify the handler method via the android:onclick attribute. Advantages Assuming that the app has only a single control of that Listener type, mostly the same advantages (short/simple code) as the previous approach where the Activity implemented the interface. More consistent with the do layout in XML strategy You can supply different method names for different controls, so not nearly as limited as interface approach. Disadvantages You cannot pass arguments to Listener. Less clear to the Java developer which method is the handler for which control Since no @Override, no warning until run time if method is spelled wrong or has wrong argument signature 20

Handling Events by Specifying the Event Handler Method in Layout Radio Button public class MainActivity extends Activity { public void CustomMethod(View arg0) { <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".mainactivity" > Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side. To create each radio button option, create a RadioButton in your layout. However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup. By grouping them together, the system ensures that only one radio button can be selected at a time <Button android:id="@+id/button1" android:onclick="custommethod" android:text="button" /> 4T025-1-A </RelativeLayout> @ Peter Lo 2014 This is the name of the event handler method in the main class. This method must have a void return type and take a View as an argument. However, the method name is arbitrary, and the main class need not implement any particular interface. 21 activity_main.xml 22 Handling Events in Radio Button Checkbox RadioButton radio0 = (RadioButton) findviewbyid(r.id.radio0); RadioButton radio1 = (RadioButton) findviewbyid(r.id.radio1); RadioGroup radiogroup1 = (RadioGroup) findviewbyid(r.id.radiogroup1); radiogroup1.setoncheckedchangelistener( new RadioGroup.OnCheckedChangeListener() { public void oncheckedchanged ( RadioGroup group, int checkedid ) { if ( checkedid == radio0.getid() ) { else if ( checkedid == radio1.getid() ) { ); Checkboxes allow the user to select one or more options from a set. Typically, you should present each checkbox option in a vertical list. To create each checkbox option, create a CheckBox in your layout. Because a set of checkbox options allows the user to select multiple items, each checkbox is managed separately and you must register a click listener for each one. 23 24

Handling Events in Checkbox Permission Concept in Android CheckBox checkbox1 = (CheckBox) findviewbyid(r.id.checkbox1); if (checkbox1.ischecked()) { else { CheckBox checkbox2 = (CheckBox) findviewbyid(r.id.checkbox2); if (checkbox2.ischecked()) { else { 25 Android contains a permission system and predefines permissions for certain tasks. Every application can request required permissions and also define new permissions. E.g. an application may declare that it requires access to the Internet. Permissions have different levels. Some permissions are automatically granted by the Android system Some are automatically rejected. In most cases the requested permissions are presented to the user before installing the application. The user needs to decide if these permissions shall be given to the application. An Android application declares the required permissions in its AndroidManifest.xml configuration file. It can also define additional permissions which it can use to restrict access to certain components. 26 Using Permissions Common Permission A basic Android application has no permissions associated with it by default, meaning it cannot do anything that would adversely impact the user experience or any data on the device. To make use of protected features of the device, you must include in your AndroidManifest.xml one or more <uses-permission> tags declaring the permissions that your application needs The permissions provided by the Android system can be found at http://developer.android.com/reference/android/manifest.permission.html 27 Permission ACCESS_FINE_LOCATION BLUETOOTH CAMERA INTERNET READ_CONTACTS READ_EXTERNAL_STORAGE Description Allows an app to access precise location from location sources such as GPS, cell towers, and Wi-Fi. Allows applications to connect to paired Bluetooth devices Required to be able to access the camera device. Allows applications to open network sockets. Allows an application to read the user's contacts data. Allows an application to read from external storage. WRITE_EXTERNAL_STORAGE Allows an application to write to external storage. NFC CALL_PHONE Allows applications to perform I/O operations over NFC Allows an application to initiate a phone call without going through the Dialer user interface for the user to confirm the call being placed. 28