18
CS260 Intro to Java & Android 07.AndroidIntents Winter 2020 Winter 2020 CS260 - Intro to Java & Android 1

CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

CS260 Intro to Java & Android07.AndroidIntents

Winter 2020

Winter 2020 CS260 - Intro to Java & Android 1

Page 2: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Application Organization

• The Android Architecture is designed so an application is composed of well-defined Activities

• One Activity is the main Activity launched by the launcher

• Each Activity is reachable via intentsWinter 2020 CS260 - Intro to Java & Android 2

Page 3: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Intent

• An intent is a messaging object used to request some action from another app component

– Activity

– Service

– Broadcast receiver

– Content provider

Winter 2020 CS260 - Intro to Java & Android 3

Page 4: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Starting an Activity

• An Activity represents a single screen in an app

• A new Activity instance can be started by passing an Intent to a start activity method

• Intent

– describes the activity to start

– contains any necessary dataWinter 2020 CS260 - Intro to Java & Android 4

Page 5: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Starting an Activity

• startActivity () starts another activity and returns no result

• startActivityForResult () returns a result as a separate Intent object in the calling activities onActivityResult () callback

Winter 2020 CS260 - Intro to Java & Android 5

Page 6: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Intent

• An intent is also a message facility for late run-time binding between components in the same or different applications

• What is binding time?

• What are possible binding times?

Winter 2020 CS260 - Intro to Java & Android 6

Page 7: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Starting new Activities

• Activities can be started:

1. explicitly – a class to load is specified

2. implicitly – an action to be performed on a piece of data is requested

Winter 2020 CS260 - Intro to Java & Android 7

Page 8: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Explicitly Starting An Activity

• One Activity shows up in the launcher

• Other Activities need to be reached somehow

• Intents are messages

• Android is about intents and receivers of intents

• Explicitly starting an Activity:startActivity (intent);

Winter 2020 CS260 - Intro to Java & Android 8

Page 9: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Two Explicit Scenerios

Consider Activity (A1) launches Activity (A2)

• Question: Does A1 need a result from A2?

• If so, then launch A2 as a sub-activity so A1 knows when A2 is done

• If not, then launch A2 as a regular ActivityWinter 2020 CS260 - Intro to Java & Android 9

Page 10: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Explicit Activity Startup

• Activity startup requires:

– an intent

– a choice of how to start the Activity

• Remember, intents “encapsulate a request” for some other component (Activity right now) to do something

Winter 2020 CS260 - Intro to Java & Android 10

Page 11: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

startActivity• The easiest way to start an Activity is:

startActivity (new Intent (this, Classname.class);

The arguments for Intent in the above case are:

this – a Context of the application package implementing the class

Classname.class – the component class that is to be used for the intent

Winter 2020 CS260 - Intro to Java & Android 11

Page 12: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Remember

• The previous statement will launch the Activity Classname

• You MUST make sure the Activity classnameexists in the AndroidManifest.xml file. This will be automagic if you add the Activity correctly.

Winter 2020 CS260 - Intro to Java & Android 12

Page 13: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Problem #11. When the Button HELP is pressed in the main

Activity of your Addition application, you are to start an Activity that displays the following:

Pressing COMPUTE - performs the addition

Pressing CLEAR - clears all input fields

Winter 2020 CS260 - Intro to Java & Android 13

Page 14: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Problem #1How?

1. Add a second Blank Activity to the package called Help. A layout called activity_help.xml will be created.

2. Modify MainActivity.java to start the new Activity Help when the HELP button is pressed

Winter 2020 CS260 - Intro to Java & Android 14

Page 15: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

JAR Files• Java applications can be packaged in a Java archive

(JAR) file

• JAR files

– package file format containing classes, resources, ... in one file for distribution

– can be shared with other developers

– in IntelliJ are called an "artifact"

– can be run in a command prompt byjava -jar file.jar

Winter 2020 CS260 - Intro to Java & Android 15

Page 16: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Problem #21. Copy the folder Addition from CS260-01Public\2020

on turing

2. Run the program to make sure it works

3. Follow the link "Creating JAR files" on the course Web site

4. From the command prompt, find the JAR file in the artifacts folder and run

Winter 2020 CS260 - Intro to Java & Android 16

Page 17: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Problem #3Time to hook up your Java Addition with your Android MyAddition

1. Using Windows File Explorer, find the libs folderMyAddition->app->libs

2. Place a copy of the jar file in the libs folder

3. File->Project Structure->Dependencies

4. Select app

5. Under Declared Dependencies hit the + then Jar Dependency

6. You should be able to finish from there

Winter 2020 CS260 - Intro to Java & Android 17

Page 18: CS260 Intro to Java & Android 07.AndroidIntentszeus.cs.pacificu.edu/.../07.AndroidIntents.IP.pdf · •The Android Architecture is designed so an application is composed of well-defined

Problem #3 Continued7. In your Android application MyAddition, create a

new Addition object using the Jar file

8. Call the getter from the Addition object to add two numbers

9. Pass to setText

10. Test the app

This is how you will hook up Minesweeper Java with Minesweeper Android. Pretty cool huh???

Winter 2020 CS260 - Intro to Java & Android 18