40
Lab 1 – Introduction to Android & Hello World Example KUAN-TING LAI 2018/9/10

Lab 1 – Introduction to Android & Hello World Example

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lab 1 – Introduction to Android & Hello World Example

Lab 1 – Introduction to Android & Hello World Example K U A N - T I N G L A I

2 0 1 8 / 9 / 1 0

Page 2: Lab 1 – Introduction to Android & Hello World Example

Android History

Page 3: Lab 1 – Introduction to Android & Hello World Example

Android versions

Codename

Versionnumber

Linux kernelversion[1]

Initial releasedate

APIlevel

(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.04.4.107, 4.9.84, and 4.14.42

August 6, 2018 28

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

Page 4: Lab 1 – Introduction to Android & Hello World Example

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

Page 5: Lab 1 – Introduction to Android & Hello World Example

New Android Language: Kotlin

Page 6: Lab 1 – Introduction to Android & Hello World Example

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

Page 7: Lab 1 – Introduction to Android & Hello World Example

APP Components

Activities Handle UI and interact with user Ex: A photo app calls an email app to share photos

Services Run background processEx: Music playback

Broadcast Receivers Receive system eventsEx: Alarm, battery low, …

Content Providers Manage APP dataEx: SQLite database

Page 8: Lab 1 – Introduction to Android & Hello World Example

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

Page 9: Lab 1 – Introduction to Android & Hello World Example

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

Page 10: Lab 1 – Introduction to Android & Hello World Example

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

Page 11: Lab 1 – Introduction to Android & Hello World Example

Broadcast Receivers

• Send or receive broadcast messages from the Android system and other Android apps

• Publish-subscribe design pattern

• Messages are wrapped in Intent

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

Page 12: Lab 1 – Introduction to Android & Hello World Example

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

Page 13: Lab 1 – Introduction to Android & Hello World Example

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

Page 14: Lab 1 – Introduction to Android & Hello World Example

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 of the views

Intents Messages wiring components together

Resources External elements, such as strings, constants and drawable pictures

Manifest Configuration file for the application

Page 15: Lab 1 – Introduction to Android & Hello World Example

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

Page 16: Lab 1 – Introduction to Android & Hello World Example

Create an Android Project

Page 17: Lab 1 – Introduction to Android & Hello World Example

Select Default API

Page 18: Lab 1 – Introduction to Android & Hello World Example

Select Empty Activity

Page 19: Lab 1 – Introduction to Android & Hello World Example

Use Default Name

Page 20: Lab 1 – Introduction to Android & Hello World Example

Project Files

• app > java > com.aiotlab.helloworld > MainActivity

• app > res > layout > activity_main.xml

• app > manifests > AndroidManifest.xml

• Gradle Scripts > build.gradle

Page 21: Lab 1 – Introduction to Android & Hello World Example

Run Your App

• On real device (Your phone)◦ Enable USB debugging

◦ Settings -> System -> About Phone -> Build number => CLICK 7 TIMES

◦ Click Run

Page 22: Lab 1 – Introduction to Android & Hello World Example

Create Android Virtual Device

• Click Run

• Select Deployment Target dialog will appear =>

• Create New Virtual Device

Page 23: Lab 1 – Introduction to Android & Hello World Example

Download System Image

Click Download

Page 24: Lab 1 – Introduction to Android & Hello World Example

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

Page 25: Lab 1 – Introduction to Android & Hello World Example

Today’s Assignment

Page 26: Lab 1 – Introduction to Android & Hello World Example

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

Page 27: Lab 1 – Introduction to Android & Hello World Example

Hierarchy of Layouts

Page 28: Lab 1 – Introduction to Android & Hello World Example

1. activity_main.xml

2. design

3. Click Select Blueprint

4. Default margin 16

5. Turn off autoconnect

6. Show constraints

Page 29: Lab 1 – Introduction to Android & Hello World Example

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”

Page 30: Lab 1 – Introduction to Android & Hello World Example

2. Select both PlainText & Button

3. Right click -> Chain > Create

Horizontal Chain

1. Drag & drop PlainText & Button

Page 31: Lab 1 – Introduction to Android & Hello World Example

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

Page 32: Lab 1 – Introduction to Android & Hello World Example

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

}

}

Page 33: Lab 1 – Introduction to Android & Hello World Example

Assign sendMessage() to Button onClick

Page 34: Lab 1 – Introduction to Android & Hello World Example

Build an Intentpublic 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);

}

}

Page 35: Lab 1 – Introduction to Android & Hello World Example

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 DisplayMessageActivityspecified by the Intent

Page 36: Lab 1 – Introduction to Android & Hello World Example

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).

Page 37: Lab 1 – Introduction to Android & Hello World Example

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 of the layout, so it appears as shown in figure below

Page 38: Lab 1 – Introduction to Android & Hello World Example

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);

}

Page 39: Lab 1 – Introduction to Android & Hello World Example

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>

Page 40: Lab 1 – Introduction to Android & Hello World Example

Final Result

• Send “Hello World!” message in your APP!

• Show your result to TA