Lab 1 Introduction to Android & Hello World Example KUAN-TING - - PowerPoint PPT Presentation

lab 1 introduction to android hello world example
SMART_READER_LITE
LIVE PREVIEW

Lab 1 Introduction to Android & Hello World Example KUAN-TING - - PowerPoint PPT Presentation

Lab 1 Introduction to Android & Hello World Example KUAN-TING LAI 2018/9/10 Android History Code Version Linux kernel Initial release API version [1] name number date level (No codename) [2] 1.0 ? September 23, 2008 1 Petit


slide-1
SLIDE 1

Lab 1 – Introduction to Android & Hello World Example

KUAN-TING LAI 2018/9/10

slide-2
SLIDE 2

Android History

slide-3
SLIDE 3

Android versions

Code name Version number Linux kernel version[1] Initial release date API level (No codename)[2] 1.0 ? September 23, 2008 1 Petit Four[2] 1.1 2.6.X February 9, 2009 2 Cupcake 1.5 2.6.27 April 27, 2009 3 Donut[3] 1.6 2.6.29 September 15, 2009 4 Eclair[4] 2.0 – 2.1 2.6.29 October 26, 2009 5 – 7 Froyo[5] 2.2 – 2.2.3 2.6.32 May 20, 2010 8 Gingerbread[6] 2.3 – 2.3.7 2.6.35 December 6, 2010 9 – 10 Honeycomb[7] 3.0 – 3.2.6 2.6.36 February 22, 2011 11 – 13 Ice Cream Sandwich[8] 4.0 – 4.0.4 3.0.1 October 18, 2011 14 – 15 Jelly Bean[9] 4.1 – 4.3.1 3.0.31 to 3.4.39 July 9, 2012 16 – 18 KitKat[10] 4.4 – 4.4.4 3.10 October 31, 2013 19 – 20 Lollipop[11] 5.0 – 5.1.1 3.16.1 November 12, 2014 21 – 22[12] Marshmallow[13] 6.0 – 6.0.1 3.18.10 October 5, 2015 23 Nougat[14] 7.0 – 7.1.2 4.4.1 August 22, 2016 24 – 25 Oreo[15] 8.0 – 8.1 4.10 August 21, 2017 26 – 27 Pie[16] 9.0 4.4.107, 4.9.84, and 4.14.42 August 6, 2018 28

Courtesy: https://en.wikipedia.org/wiki/Android_version_history

slide-4
SLIDE 4

Platform Architecture

  • Linux Kernel
  • Hardware Abstraction Layer (HAL)
  • Android Runtime (ART)
  • Native C/C++ Libraries
  • Java API Framework

❖ View System ❖ Resource Manager ❖ Notification Manager ❖ Activity Manager ❖ Content Providers

slide-5
SLIDE 5

New Android Language: Kotlin

slide-6
SLIDE 6

Application Fundamentals

  • The Android operating system is a multi-user Linux system
  • By default, the system assigns each app a unique Linux user ID
  • Each process has its own virtual machine (VM)
  • Every app runs in its own Linux process

https://developer.android.com/guide/components/fundamentals

slide-7
SLIDE 7

APP Components

Activities Handle UI and interact with user Ex: A photo app calls an email app to share photos Services Run background process Ex: Music playback Broadcast Receivers Receive system events Ex: Alarm, battery low, … Content Providers Manage APP data Ex: SQLite database

slide-8
SLIDE 8

https://developer.android.com/jetpack/docs/guide

slide-9
SLIDE 9

Activities

  • Activity enables one app to invoke another app
  • One screen, one activity
  • Use Intent to communicate

https://developer.android.com/guide/components/activities/intro-activities#java

slide-10
SLIDE 10

Services

  • Running in background
  • Create a background service
  • Send work requests to a service
  • Report work status
  • Bound services

https://developer.android.com/guide/components/services

slide-11
SLIDE 11

Broadcast Receivers

  • Send or receive broadcast messages from the Android system and
  • ther Android apps
  • Publish-subscribe design pattern
  • Messages are wrapped in Intent

https://developer.android.com/guide/components/broadcasts

slide-12
SLIDE 12

Content Providers

  • Sharing data with other apps
  • Sending data to a widget
  • Returning custom search

suggestions through the search framework using SearchRecentSuggestionsProvider

  • Synchronizing application data

with your server using an implementation of AbstractThreadedSyncAdapter

  • Loading data in your UI using a

CursorLoader

slide-13
SLIDE 13

Intent and Intent Filters

  • A message object used to invoke other components
  • Starting an activity
  • Starting an service
  • Delivering a broadcast
  • Explicit intents and implicit intents

https://developer.android.com/guide/components/intents-filters

Implicit Intent Delivery

slide-14
SLIDE 14

Other Components

Fragments Represent a behavior or a portion of user interface in an Activity Views UI elements that are drawn onscreen including buttons, lists forms etc. Layouts View hierarchies that control screen format and appearance

  • f the views

Intents Messages wiring components together Resources External elements, such as strings, constants and drawable pictures Manifest Configuration file for the application

slide-15
SLIDE 15

Install Android Studio (developer.android.com/studio)

slide-16
SLIDE 16

Create an Android Project

slide-17
SLIDE 17

Select Default API

slide-18
SLIDE 18

Select Empty Activity

slide-19
SLIDE 19

Use Default Name

slide-20
SLIDE 20

Project Files

  • app > java >

com.aiotlab.helloworld > MainActivity

  • app > res > layout >

activity_main.xml

  • app > manifests >

AndroidManifest.xml

  • Gradle Scripts >

build.gradle

slide-21
SLIDE 21

Run Your App

  • On real device (Your phone)
  • Enable USB debugging
  • Settings -> System -> About Phone -> Build number => CLICK 7 TIMES
  • Click Run
slide-22
SLIDE 22

Create Android Virtual Device

  • Click Run
  • Select Deployment Target dialog will appear =>
  • Create New Virtual Device
slide-23
SLIDE 23

Download System Image

Click Download

slide-24
SLIDE 24

Run Image

  • If you encounter the error below:
  • Emulator: PANIC: Cannot find AVD system path. Please define ANDROID_SDK_ROOT
  • Emulator: Process finished with exit code 1
  • https://stackoverflow.com/questions/39645178/panic-broken-avd-

system-path-check-your-android-sdk-root-value

  • Go to Menu -> File -> Project Structure to find your SDK root
  • Add ANDROID_SDK_ROOT to system variable
slide-25
SLIDE 25

Today’s Assignment

slide-26
SLIDE 26

Simple Text Sending APP

  • Create a layout that includes a text box and a button
  • Sending the content of the text box to another activity

https://developer.android.com/training/basics/firstapp/building-ui

slide-27
SLIDE 27

Hierarchy of Layouts

slide-28
SLIDE 28
  • 1. activity_main.xml
  • 2. design
  • 3. Click Select Blueprint
  • 4. Default margin 16
  • 5. Turn off autoconnect
  • 6. Show constraints
slide-29
SLIDE 29

Change UI Strings

  • app > res > values >

strings.xml

  • strings.xml -> Open Editor
  • Add Key
  • Add two string pairs (key

=> value):

  • edit_message => “Enter a

message”

  • button_send => “Send”
slide-30
SLIDE 30
  • 2. Select both PlainText & Button
  • 3. Right click -> Chain > Create

Horizontal Chain

  • 1. Drag & drop PlainText & Button
slide-31
SLIDE 31

Set Button & PlainText Margin

  • Select the button and open the Attributes window
  • Set right margin to 16
  • Select PlainText and set right margin to Match Constraints 1
slide-32
SLIDE 32

Start Another Activity

  • Open MainActivity.java and add “sendMessage()”
  • Auto fix error (Alt + Enter)

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** Called when the user taps the Send button */ public void sendMessage(View view) { // Do something in response to button } }

slide-33
SLIDE 33

Assign sendMessage() () to Button onClick

slide-34
SLIDE 34

Build an Intent

public class MainActivity extends AppCompatActivity { public static final String EXTRA_MESSAGE = "com.aiotlab.kt.helloworld.MESSAGE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** Called when the user taps the Send button */ public void sendMessage(View view) { Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.editText); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); } }

slide-35
SLIDE 35

Explain code in SendMessage()

  • The Intent constructor takes two parameters:
  • A Context as its first parameter (this is used because the Activity class is a subclass of

Context)

  • The Class of the app component to which the system should deliver the Intent
  • The putExtra() method adds the EditText's value to the intent. An Intent can

carry data types as key-value pairs called extras.

  • Your key is a public constant EXTRA_MESSAGE because the next activity

uses the key to retrieve the text value

  • Define keys for intent extras using your app's package name as a prefix to

make unique keys

  • The startActivity() method starts an instance of the DisplayMessageActivity

specified by the Intent

slide-36
SLIDE 36

Create DisplayMessageActivity

  • In the Project window, right-click the app folder and select New >

Activity > Empty Activity.

  • In the Configure Activity window, enter "DisplayMessageActivity"

for Activity Name and click Finish (leave all other properties set to the defaults).

slide-37
SLIDE 37

Add a TextView to DisplayMessageActivity

  • Open the file app > res > layout > activity_display_message.xml
  • In the Palette window, click Text and then drag a TextView into the

layout

  • Create one more constraint from the top of the text view to the top
  • f the layout, so it appears as shown in figure below
slide-38
SLIDE 38

Display the Message

  • Add the following code in “DisplayMessageActivity.java”

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); // Get the Intent that started this activity and extract the string Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); // Capture the layout's TextView and set the string as its text TextView textView = findViewById(R.id.textView); textView.setText(message); }

slide-39
SLIDE 39

Add Navigation

  • Open the file at app > manifests > AndroidManifest.xml

<activity android:name=".DisplayMessageActivity" android:parentActivityName=".MainActivity"> <!-- The meta-data tag is required if you support API level 15 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity>

slide-40
SLIDE 40

Final Result

  • Send “Hello World!” message in your APP!
  • Show your result to TA