Android Dr Vaishali D Khairnar IT Department Terna

  • Slides: 48
Download presentation
Android Dr. Vaishali D. Khairnar IT Department Terna Engineering College 1

Android Dr. Vaishali D. Khairnar IT Department Terna Engineering College 1

What is Android? p Android is a software stack for mobile devices that includes

What is Android? p Android is a software stack for mobile devices that includes an operating system, middleware and key applications. 2

OHA (Open Handset Alliance) p A business alliance consisting of 47 companies to develop

OHA (Open Handset Alliance) p A business alliance consisting of 47 companies to develop open standards for mobile devices 3

Phones HTC G 1, Droid, Tattoo Suno S 880 Motorola Droid (X) Samsung Galaxy

Phones HTC G 1, Droid, Tattoo Suno S 880 Motorola Droid (X) Samsung Galaxy Sony Ericsson 4

Tablets Velocity Micro Cruz Dawa D 7 Gome Fly. Touch Toshiba Android Smart. Book

Tablets Velocity Micro Cruz Dawa D 7 Gome Fly. Touch Toshiba Android Smart. Book Acer be. Touch Cisco Android Tablet 5

Android Studio Installation Steps p p Step 1: System Requirements n Microsoft® Windows® 8/7/Vista/2003

Android Studio Installation Steps p p Step 1: System Requirements n Microsoft® Windows® 8/7/Vista/2003 (32 or 64 -bit) n Java JDK 5 or later version (jdk 1. 8. 0_101) n Java Runtime Environment (JRE) or later version (jre 1. 8. 0_101) Set-up Java Development Kit (JDK) n set JAVA_HOME=C: jdk 1. 8. 0_101 n Android Studio (android-studio-bundle-143. 3101438 windows. exe) p Android Studio is the official IDE for android application development. It works based on. Intelli. J IDEA p Link for Android Studio developer. android. com/sdk/index. html 6

p Step 2: Installation n launch Android Studio. exe n Machine should required installed

p Step 2: Installation n launch Android Studio. exe n Machine should required installed Java JDK. n By clicking on next complete the wizard for Android Studio. n Errors p JDK error § Install latest version of jdk and configure it. p HAXM installation failed. § Download HAXM from link https: //software. intel. com/andro id/articles/installationinstructions-for-intel-hardwareaccelerated-execution-managerwindows (haxmwindows_v 6_0_3. zip) § Extra it and run (intelhaxmandroid. exe and haxm_check. exe) 7

8

8

9

9

10

10

Hello Word Example App>res>layout>Activity_main. xml 11

Hello Word Example App>res>layout>Activity_main. xml 11

Architecture 12

Architecture 12

Android S/W Stack - Application p Android provides a set of core applications: ü

Android S/W Stack - Application p Android provides a set of core applications: ü ü ü ü p Email Client SMS Program Calendar Maps Browser Contacts Etc All applications are written using the Java language. 13

Application components are the essential building blocks of an Android application. These components are

Application components are the essential building blocks of an Android application. These components are loosely coupled by the application manifest file Android. Manifest. xml that describes each component of the application and how they interact. Components Description Activities They dictate the UI and handle the user interaction to the smart phone screen Services They handle background processing associated with an application. Broadcast Receivers They handle communication between Android OS and applications. Content Providers They handle data and database management issues. 14

Additional Components: used in the construction of above mentioned entities, their logic, and wiring

Additional Components: used in the construction of above mentioned entities, their logic, and wiring between them etc. Components Description Fragments Represents a portion of user interface in an Activity. Views UI elements that are drawn on-screen including buttons, lists forms etc. Layouts View hierarchies that control screen format and appearance of the views. Intents Messages wiring components together. Resources External elements, such as strings, constants and drawable pictures. Manifest Configuration file for the application. 15

Android S/W Stack – App Framework p Enabling and simplifying the reuse of components

Android S/W Stack – App Framework p Enabling and simplifying the reuse of components ü ü Developers have full access to the same framework APIs used by the core applications. Users are allowed to replace components. 16

Android S/W Stack – App Framework (Cont) p Features Feature Role View System Used

Android S/W Stack – App Framework (Cont) p Features Feature Role View System Used to build an application, including lists, grids, text boxes, buttons, and embedded web browser Content Provider Enabling applications to access data from other applications or to share their own data Resource Manager Providing access to non-code resources (localized strings, graphics, and layout files) Notification Enabling all applications to display customer alerts in the Manager status bar Activity Manager Managing the lifecycle of applications and providing a common navigation backstack 17

Android S/W Stack - Libraries p Including a set of C/C++ libraries used by

Android S/W Stack - Libraries p Including a set of C/C++ libraries used by components of the Android system p Exposed to developers through the Android application framework 18

Android S/W Stack - Runtime p Core Libraries ü ü Providing most of the

Android S/W Stack - Runtime p Core Libraries ü ü Providing most of the functionality available in the core libraries of the Java language APIs Ø Ø Ø Data Structures Utilities File Access Network Access Graphics Etc 19

Android S/W Stack – Runtime (Cont) p Dalvik Virtual Machine ü Providing environment on

Android S/W Stack – Runtime (Cont) p Dalvik Virtual Machine ü Providing environment on which every Android application runs Ø Ø ü Each Android application runs in its own process, with its own instance of the Dalvik VM. Dalvik has been written such that a device can run multiple VMs efficiently. Register-based virtual machine 20

Android S/W Stack – Runtime (Cont) p Dalvik Virtual Machine (Cont) ü Executing the

Android S/W Stack – Runtime (Cont) p Dalvik Virtual Machine (Cont) ü Executing the Dalvik Executable (. dex) format Ø Ø ü . dex format is optimized for minimal memory footprint. Compilation Relying on the Linux Kernel for: Ø Ø Threading Low-level memory management 21

Android S/W Stack – Linux Kernel l l Relying on Linux Kernel 2. 6

Android S/W Stack – Linux Kernel l l Relying on Linux Kernel 2. 6 for core system services ü Memory and Process Management ü Network Stack ü Driver Model ü Security Providing an abstraction layer between the H/W and the rest of the S/W stack 22

Create Android Application (Hello. World) p Click on Start a new Android Studio Project

Create Android Application (Hello. World) p Click on Start a new Android Studio Project 23

Next, follow the instructions provided and keep all other entries as default till the

Next, follow the instructions provided and keep all other entries as default till the final step. Once your project is created successfully, you will have following project screen 24

25

25

Application S. N. Folder, File & Description 1 src This contains the. java source

Application S. N. Folder, File & Description 1 src This contains the. java source files for your project. By default, it includes a Main. Activity. java source file having an activity class that runs when your app is launched using the app icon. 2 gen This contains the. R file, a compiler-generated file that references all the resources found in your project. You should not modify this file. 3 bin This folder contains the Android package files. apk built by the ADT during the build process and everything else needed to run an Android application. 4 res/drawable-hdpi This is a directory for drawable objects that are designed for high-density screens. 5 res/layout This is a directory for files that define your app's user interface. 6 res/values This is a directory for other various XML files that contain a collection of resources, such as strings and colours definitions. 7 Android. Manifest. xml This is the manifest file which describes the fundamental characteristics of the app and defines each of its components. 26

The Main Activity File p The main activity code is a Java file Main.

The Main Activity File p The main activity code is a Java file Main. Activity. java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application. Following is the default code generated by the application wizard for Hello World! application − p package com. example. hod_it. helloworld; import android. support. v 7. app. App. Compat. Activity; import android. os. Bundle; public class Main. Activity extends App. Compat. Activity { @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_main); } } p R. layout. activity_main refers to the activity_main. xml file located in the res/layout folder. The on. Create() method is one of many methods that are figured when an activity is loaded. 27

The Manifest File p <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas.

The Manifest File p <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="com. example. hod_it. helloworld"> <application android: allow. Backup="true" android: icon="@mipmap/ic_launcher" android: label="@string/app_name" android: supports. Rtl="true" android: theme="@style/App. Theme"> <activity android: name=". Main. Activity"> <intent-filter> <action android: name="android. intent. action. MAIN" /> <category android: name="android. intent. category. LAUNCHER" /> </intent-filter> </activity> </application> </manifest> p Whatever component you develop as a part of your application, you must declare all its components in a manifest. xml which resides at the root of the application project directory. This file works as an interface between Android OS and your application, so if you do not declare your component in this file, then it will not be considered by the OS. p The Strings File p The R File p The Layout File 28

Android Activities Callback Description on. Create() This is the first callback and called when

Android Activities Callback Description on. Create() This is the first callback and called when the activity is first created. on. Start() This callback is called when the activity becomes visible to the user. on. Resume() This is called when the user starts interacting with the application. Pause() The paused activity does not receive user input and cannot execute any code and called when the current activity is being paused and the previous activity is being resumed. on. Stop() This callback is called when the activity is no longer visible. on. Destroy() This callback is called before the activity is destroyed by the system. on. Restart() This callback is called when the activity restarts after stopping it. 29

Example 30

Example 30

Android Intents p p Intents are asynchronous messages which allow application components to request

Android Intents p p Intents are asynchronous messages which allow application components to request functionality from other Android components. Intents allow you to interact with components from the same applications as well as with components contributed by other applications. For example, an activity can start an external activity for taking a picture. Intent can be used to: n n n Start an Activity Start sub-activity. Start a Service. 31

Types of Intent Explicit Intent: An application can define the target component directly in

Types of Intent Explicit Intent: An application can define the target component directly in the intent p Implicit Intent: Ask the Android system to evaluate registered components based on the intent data. p 32

Explicit Intent example 33

Explicit Intent example 33

Steps p Create "Empty Activity" project named Explicit. Intent. Example 34

Steps p Create "Empty Activity" project named Explicit. Intent. Example 34

35

35

p By double-clicking on the components on the interface, you can set the ID

p By double-clicking on the components on the interface, you can set the ID and text for them: n Edit. Text 1: ID: text_first. Name n Properties p layout_width: fill_parent n Edit. Text 2: ID: text_last. Name n Properties p layout_width: fill_parent n Text. View: ID: text_feedback n Text: <Feedback> n Properties: p layout_width: fill_parent p gravity: center_horizontal 36

n n n Button: ID: button_greeting Text: Show Greeting Properties: p on. Click: show.

n n n Button: ID: button_greeting Text: Show Greeting Properties: p on. Click: show. Greeting 37

activity_explicit_intent. xml p <? xml version="1. 0" encoding="utf-8"? > <Relative. Layout xmlns: android="http: //schemas.

activity_explicit_intent. xml p <? xml version="1. 0" encoding="utf-8"? > <Relative. Layout xmlns: android="http: //schemas. android. com/apk/res/android" xmlns: tools="http: //schemas. android. com/tools" android: layout_width="match_parent" android: layout_height="match_parent" android: padding. Bottom="@dimen/activity_vertical_margin" android: padding. Left="@dimen/activity_horizontal_margin" android: padding. Right="@dimen/activity_horizontal_margin" android: padding. Top="@dimen/activity_vertical_margin" tools: context="com. example. hod_it. explicitintentexample. Explicit. Intent. Activity" > <Text. View android: layout_width="fill_parent" android: layout_height="wrap_content" android: text. Appearance="? android: attr/text. Appearance. Large" android: text="< Feedback>" android: id="@+id/text_feedback" android: layout_align. Parent. Bottom="true" android: layout_margin. Bottom="92 dp" android: single. Line="true" android: layout_center. Horizontal="true" android: text. Alignment="center" android: gravity="center_horizontal" /> <Button android: layout_width="wrap_content" android: layout_height="wrap_content" android: text="Show Greeting" android: id="@+id/button_greeting" android: on. Click="show. Greeting" android: layout_above="@+id/text_feedback" android: layout_center. Horizontal="true" android: layout_margin. Bottom="68 dp" /> <Edit. Text android: layout_width="fill_parent" android: layout_height="wrap_content" android: id="@+id/text_firstname" android: layout_center. Horizontal="true" android: layout_margin. Top="40 dp" /> <Edit. Text android: layout_width="fill_parent" android: layout_height="wrap_content" android: id="@+id/text_lastname" android: layout_below="@+id/text_firstname" android: layout_center. Horizontal="true" android: layout_margin. Top="51 dp" /> </Relative. Layout> 38

When users click the button, the program will call a other Activity displays a

When users click the button, the program will call a other Activity displays a greeting. You need to create an Activity with name Greeting. Activity . 39

40

40

p Change the attributes for components on the interface: n n n Text. View

p Change the attributes for components on the interface: n n n Text. View ID: text_greeting Text: <Greeting> Properties: p layout_width: fill_parent p gravity: center_horizontal Button: ID: button_back n Properties p on. Click: back. Clicked 41

activity_greeting. xml p <? xml version="1. 0" encoding="utf-8"? > <Relative. Layout xmlns: android="http: //schemas.

activity_greeting. xml p <? xml version="1. 0" encoding="utf-8"? > <Relative. Layout xmlns: android="http: //schemas. android. com/apk/res/android" xmlns: tools="http: //schemas. android. com/tools" android: layout_width="match_parent" android: layout_height="match_parent" android: padding. Bottom="@dimen/activity_vertical_margin" android: padding. Left="@dimen/activity_horizontal_margin" android: padding. Right="@dimen/activity_horizontal_margin" android: padding. Top="@dimen/activity_vertical_margin" tools: context="com. example. hod_it. explicitintentexample. Greeting. Activity"> <Text. View android: layout_width="fill_parent" android: layout_height="wrap_content" android: text. Appearance="? android: attr/text. Appearance. Large" android: text="< Greeting>" android: id="@+id/text_greeting" android: layout_align. Parent. Top="true" android: layout_center. Horizontal="true" android: layout_margin. Top="65 dp" android: text. Alignment="center" android: gravity="center_horizontal"/> <Button android: layout_width="wrap_content" android: layout_height="wrap_content" android: text="Back" android: id="@+id/button_back" android: layout_align. Parent. Bottom="true" android: layout_center. Horizontal="true" android: layout_margin. Bottom="165 dp" android: on. Click="back. Clicked" /> </Relative. Layout> 42

Explicit. Intent. Activity. java p package com. example. hod_it. explicitintentexample; import android. support. v

Explicit. Intent. Activity. java p package com. example. hod_it. explicitintentexample; import android. support. v 7. app. App. Compat. Activity; import android. os. Bundle; import android. app. Activity; import android. content. Intent; import android. view. View; import android. widget. Edit. Text; import android. widget. Text. View; import android. widget. Toast; public class Explicit. Intent. Activity extends App. Compat. Activity { private Edit. Text text. First. Name; private Edit. Text text. Last. Name; private Text. View text. Feedback; public static final int MY_REQUEST_CODE = 100; @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_explicit_intent); this. text. First. Name = (Edit. Text)this. find. View. By. Id(R. id. text_firstname); this. text. Last. Name = (Edit. Text)this. find. View. By. Id(R. id. text_lastname); this. text. Feedback = (Text. View)this. find. View. By. Id(R. id. text_feedback); } // When 'Greeting Activity' completed, it sends back a feedback. // (If you have started it by start. Activity. For. Result()) @Override protected void on. Activity. Result(int request. Code, int result. Code, Intent data) { if (result. Code == Activity. RESULT_OK && request. Code == MY_REQUEST_CODE ) { String feedback = data. get. String. Extra("feedback"); this. text. Feedback. set. Text(feedback); } else { this. text. Feedback. set. Text("!? "); } } // The method is called when the user clicks on "Show Greeting" button. public void show. Greeting(View view) { String first. Name= this. text. First. Name. get. Text(). to. String(); String last. Name= this. text. Last. Name. get. Text(). to. String(); Intent intent = new Intent(this, Greeting. Activity. class); intent. put. Extra("first. Name", first. Name); intent. put. Extra("last. Name", last. Name); // Start Activity and no need feedback. // this. start. Activity(intent); // Start Activity and get feedback. this. start. Activity. For. Result(intent, MY_REQUEST_CODE); } } 43

Greeting. Activity. java p package com. example. hod_it. explicitintentexample; import android. app. Activity; import

Greeting. Activity. java p package com. example. hod_it. explicitintentexample; import android. app. Activity; import android. content. Intent; import android. support. v 7. app. App. Compat. Activity; import android. os. Bundle; import android. view. View; import android. widget. Text. View; public class Greeting. Activity extends App. Compat. Activity { private String first. Name; private String last. Name; @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_greeting); // Intent is passed into Intent intent = this. get. Intent(); this. first. Name= intent. get. String. Extra("first. Name"); this. last. Name = intent. get. String. Extra("last. Name"); String greeting = "Hello "+ first. Name+" "+ last. Name; Text. View text. Greeting =(Text. View) this. find. View. By. Id(R. id. text_greeting); text. Greeting. set. Text(greeting); } // When completed this Activity, send feedback to the caller. @Override public void finish() { // Prepare data intent Intent data = new Intent(); data. put. Extra("feedback", "I'm "+ this. first. Name+", Hi!"); // Activity finished ok, return the data this. set. Result(Activity. RESULT_OK, data); super. finish(); } // The method is called when the user clicks the Back button. public void back. Clicked(View view) { // Calling on. Back. Pressed(). // Gọi phương thức on. Back. Pressed(). this. on. Back. Pressed(); } } 44

Android. Manifest. xml p <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas.

Android. Manifest. xml p <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="com. example. hod_it. explicitintentexample"> <application android: allow. Backup="true" android: icon="@mipmap/ic_launcher" android: label="@string/app_name" android: supports. Rtl="true" android: theme="@style/App. Theme"> <activity android: name=". Explicit. Intent. Activity"> <intent-filter> <action android: name="android. intent. action. MAIN" /> <category android: name="android. intent. category. LAUNCHER" /> </intent-filter> </activity> <activity android: name=". Greeting. Activity"></activity> </application> </manifest> 45

Home-Work: Add Middle Name in First Screen Display First Name, Middle Name and Last

Home-Work: Add Middle Name in First Screen Display First Name, Middle Name and Last Name With Message Hello How are you!!!. 46

Implicit intent p Implicit intents: These do not specify a target component, but include

Implicit intent p Implicit intents: These do not specify a target component, but include enough information for the system to determine which of the available components is best to run for that intent. p Consider an app that lists the available restaurants near you. When you click a particular restaurant option, the application has to ask another application to display the route to that restaurant. To achieve this, it could either send an explicit intent directly to the Google Maps application, or send an implicit intent, which would be delivered to any application that provides the Maps� functionality (e. g. , Google Maps, Yahoo Maps). 47

Example with the implicit intent p In this example, you will click a Button

Example with the implicit intent p In this example, you will click a Button which will show you a website by URL, you create an implicitly Intent, Intent sent to the Android system to decide which components will be opened , maybe in your equipment installs many different browsers (Firefox, Chrome, . . ), the device will open it in your default browser or your preferred browser p In addition examples also include: Send an email 48