Download presentation
Presentation is loading. Please wait.
Published byAdele Tyler Modified over 9 years ago
1
1 Mobile Computing Java, Android, and Eclipse Copyright 2015 by Janson Industries
2
2 Objectives ▀ Explain u Application organization in Eclipse u Basic Java concepts u How to code and run an Android app in the emulator u Controlling the emulator u The Android app life cycle
3
Copyright 2015 by Janson Industries 3 Eclipse ▀ Has a variety of perspectives u Java, Debug, DDMS ▀ Each perspective consists of a unique set of functions and views of the application resources u Java shows source code and allows the programmer to edit it u Debug shows the stack trace (logic flow) of a running app u DDMS allows access to the device/emulator’s file system
4
Copyright 2015 by Janson Industries 4 Perspective indicated in upper right hand corner Java Perspective window initially consists of 6 panes – only need 4. Can close Task List and Connect Mylyn Resize panes by clicking and dragging borders Double click view tab to expand view and fill perspective window Panes contain views Views indicated by tabs at top of pane, switch view by clicking tab
5
Copyright 2015 by Janson Industries 5 Can have multiple perspectives open but only one is active
6
Copyright 2015 by Janson Industries 6 Open a new perspective by clicking Window, Open Perspective, then choose a perspective Switch between perspectives by clicking perspective button
7
Copyright 2015 by Janson Industries 7 Eclipse ▀ All of an application’s resources are stored in a project u Source code u Images u XML ▀ The resources can be further organized into folders and packages
8
Copyright 2015 by Janson Industries 8 Project Package Folder Package Folder File Eclipse ▀ Packages and folders hold the majority of an application’s resources u Source code u Images u XML ▀ Java source code must go into a package
9
Copyright 2015 by Janson Industries 9 Android Java ▀ An Android application’s programs are called activities ▀ Files with an extension of.java hold an activity’s source code ▀ To create an activity you have to have a project and a package to put it in
10
Copyright 2015 by Janson Industries 10 Creating an Application ▀ Click File, New and then Project ▀ Select Android Project ▀ Specify: u Project, package, activity and application names u A build target
11
Copyright 2015 by Janson Industries 11 Click File, New, and Project then expand Android, select Android Application Project and click Next
12
Copyright 2015 by Janson Industries 12 Give names to the Application (Howdy), Project (MyFirstProject), Package (my.first.pkg), specify 2.1, click Next
13
Copyright 2015 by Janson Industries 13 n Click Next Creating an Application
14
Copyright 2015 by Janson Industries 14 n Can create a unique icon n We’ll accept default and click Next Creating an Application
15
Copyright 2015 by Janson Industries 15 n Will create a Hello World activity for us n Click Next Creating an Application
16
Copyright 2015 by Janson Industries 16 n Change activity name to HowdyActivity n Click Finish Creating an Application
17
Copyright 2015 by Janson Industries 17 Creating an Application n Eclipse will create the u Project u Packages and folders u Files n It even creates a working application u In a file called HowdyActivity.java u File stored in a package called my.first.pkg in source folder src
18
Copyright 2015 by Janson Industries 18 To Run an Application n In Package Explorer, expand MyFirstProject, src, & my.first.pkg n Right click HowdyActivity and select Run As then Run Configurations n Select Android Application and click the New button
19
Copyright 2015 by Janson Industries 19
20
Copyright 2015 by Janson Industries 20 Give the configuration a name and specify the Project Click Apply and Run
21
Copyright 2015 by Janson Industries 21 To Run an Application n First time will take a while n Emulator must configure itself and will launch n The emulator is displayed u You have to click and drag the lock icon to see the results of the activity
22
Copyright 2015 by Janson Industries 22 Click and drag the lock icon to the right
23
Copyright 2015 by Janson Industries 23 If app doesn’t start, rerun in Eclipse
24
Copyright 2015 by Janson Industries 24 Console should show that app was installed just not run
25
Copyright 2015 by Janson Industries 25 Voila!
26
Copyright 2015 by Janson Industries 26 To Run an Application n Once emulator is running, results will be shown must faster n To close emulator: u Right click Android button in the System Tray u Select Close Window u Or click the Close Window button n Can control the size of the emulator
27
Copyright 2015 by Janson Industries 27 Control Emulator Size n Run emulator before running an activity. Click: u Window (in the command bar) u AVD Manager u Select the AVD u Click Start u Click Scale display to real size u Specify screen size u Click Launch
28
Copyright 2015 by Janson Industries 28 1 2 3 4 5
29
Copyright 2015 by Janson Industries 29 If it shows, accept the default video source
30
Copyright 2015 by Janson Industries 30 New size fits on the screen better Need to go back and close AVD Manager window
31
Copyright 2015 by Janson Industries 31 How Does It Work n The generated application is pretty complicated and requires a lot of Java knowledge n Let’s first learn some Java (then some XML) and then generate our own application u Later we will cover the workings of the generated application
32
Copyright 2015 by Janson Industries 32 Java n Java programs are called classes n Classes are stored in files that have an extension of.java n Classes are comprised of a header and a body
33
Copyright 2015 by Janson Industries 33 Java Class n Class header defines: u The source code as a class (e.g. “class” keyword is used) u Access allowed (e.g. “public”) u The name of class F Must begin with an upper case letter F Is case sensitive F Cannot contain spaces F Must match.java file name prefix I.e. a class named Customer must be in a file named Customer.java
34
Copyright 2015 by Janson Industries 34 Java Class n The class body is enclosed in braces {} and comprised of class/global variables and methods n Simple class example: public class ClassName { global variable definition method{} }
35
Copyright 2015 by Janson Industries 35 Java Method n Comprised of a header and body n Header definition comprised of: u Modifiers (e.g. “private”, “public”, “static”) u Return value (e.g. “void”, “String”) u Method name F Begins with a lower case letter (e.g. getMailingLabel, main) u Parameter(s)/received value(s) in parenthesis (e.g. (String name), (int age), () means no params)
36
Copyright 2015 by Janson Industries 36 Java Method Header n Method header/definition examples: u public void setName(String custName) u public String getMailingLabel() u public static void main(String[ ] args) F A static method can be run all by itself’ It is self sufficient/stand alone n If multiple values passed/received simply separate by commas u (String itemName, int itemPrice)
37
Copyright 2015 by Janson Industries 37 Java Method Header n Private methods: u Can only be accessed/run by other methods within the class n Public methods: u Can be accessed by objects external to the class u Are considered the class “interface” F This is how other classes/objects can interface with this class
38
Copyright 2015 by Janson Industries 38 Java Method Body n Enclosed in braces { } n Comprised of: u Local variable definitions u Executable statements n Variable definition comprised of: u The variable type u The variable name u A semicolon (;)
39
Copyright 2015 by Janson Industries 39 Java Method Body n Variable definition examples int age; String customerName; double salary; String street, city, state, zip; n In addition, variable definitions can u Specify access modifiers u Initialize the variable
40
Copyright 2015 by Janson Industries 40 Java Method Body n Variable definition examples private int age = 22; String customerName = new String( “Joe”) ; double salary = 1123.54; public String street, city, state, zip; n Executable statements also must end in semicolons u System.out.println(“Howdy”);
41
Copyright 2015 by Janson Industries 41 Java Method Body Example public void onCreate(Bundle aBundle) { super.onCreate(aBundle); String greeting = new String(“******Hello*******”); System.out.println(“A print of static text"); System.out.println(“The value of greeting is: ” + greeting); } statements variable definition
42
Copyright 2015 by Janson Industries 42 Classes n If a class is stored in a package, the class must have a package statement at the very beginning of the source code n So for example, the HowdyActivity class has the following: package my.first.pkg;
43
Copyright 2015 by Janson Industries 43 Activity Classes n Activity classes are executable n Some special requirements u The class must be defined as an acitivty u When first run, the activity’s onCreate method will be executed u onCreate must F accept a Bundle object F call it’s superclass’ onCreate method and pass the bundle
44
Copyright 2015 by Janson Industries 44 Superclass n All classes are related in a hierarchy n This parent/child relationship is called a superclass/subclass relationship for java classes u Just like in real life the child/subclass inherits all the parent/superclass’ variables and methods
45
Copyright 2015 by Janson Industries 45 Superclass n For example, an EditText class is a visual component that a user can enter text into n This is it’s lineage: Object View TextView EditText
46
Copyright 2015 by Janson Industries 46 Import Statements n All the java classes in the SDK are stored in packages n To use these classes you can specify the location (i.e. the package(s) that hold them u android.app.Activity u android.os.Bundle n This is called a fully qualified file reference
47
Copyright 2015 by Janson Industries 47 Import Statements n Fully qualified file references mean extra typing and more errors n If import statements are added we can use non-fully qualified file references for the classes (Activity, Bundle ) import android.app.Activity; import android.os.Bundle;
48
Copyright 2015 by Janson Industries 48 Import Statements n The import statements come after the package statement but before the class header n Now if we want to use the Bundle class we can type u Bundle n Instead of u android.os.Bundle
49
Copyright 2015 by Janson Industries 49 Import Statements n So you can use fully qualified file references like this : n Or use import statements and non- fully qualified file references like this : public void onCreate(Bundle aBundle) { public void onCreate(android.os.Bundle aBundle) {
50
Copyright 2015 by Janson Industries 50 Activity Classes n To define the class as an activity (I.e. a subclass of the Activity class) an extends clause must be included in the class header as follows: n Comments (non-executable statements) preceded by // public class HowdyActivity extends Activity {
51
Copyright 2015 by Janson Industries 51 Putting It All Together package my.first.pkg; //Identifies the package the class is in import android.app.Activity; //Identifies the location of classes import android.os.Bundle; //this class will be using public class HowdyActivity extends Activity { //Class header //Method header public void onCreate(Bundle savedInstanceState) { //Superclass’ onCreate method invoked super.onCreate(savedInstanceState); //String variable defined and initialized String greeting = new String("******Hello*******"); //Two lines of text are displayed System.out.println("A print of static text"); System.out.println("The value of greeting is: " + greeting); }
52
Copyright 2015 by Janson Industries 52 Putting It All Together n So in HowdyActivity: u Comment out the one setContentView statement F Precede the statement with // F Keystroke short cut: Select the statement Click Ctrl+/ u After the commented out statement, add the three statements from the previous slide that F Create the String variable F Display the two lines of text
53
Copyright 2015 by Janson Industries 53 Putting It All Together n Code should look like this
54
Copyright 2015 by Janson Industries 54 Running the New App n In the Package Explorer view, select the MyFirstProject n Click the Run button (green circle with white arrow head) n You’ll be prompted to save the changes n Click Yes n Nothing happened!?! u This gives us the opportunity to introduce LogCat
55
Copyright 2015 by Janson Industries 55 LogCat n LogCat holds all the system generated msgs and any println statements run in the code n If LogCat not displayed at bottom of window, display by clicking: u Window u Show View u Other u Android (to expand it) u LogCat u OK
56
Copyright 2015 by Janson Industries 56 LogCat n If your LogCat has no msgs (as below) it is because the emulator doesn’t have “focus”
57
Copyright 2015 by Janson Industries 57 Switch to DDMS perspective (Window, Open Perspective) and click on the emulator in the Devices view
58
Copyright 2015 by Janson Industries 58 If nothing appears, click the down arrow and select Reset adb Emulator will be redisplayed
59
Copyright 2015 by Janson Industries 59 Go back to the Java perspective and scroll to the right in LogCat to display all the text Common mistake: looking at the console not LogCat
60
Copyright 2015 by Janson Industries 60 If app rerun, info will not be redisplayed because app is already created Prove by clearing LogCat and rerunning
61
Copyright 2015 by Janson Industries 61 New system msgs will be there but not the text from the app This brings up the Android application life cycle!
62
Copyright 2015 by Janson Industries 62 Application Life Cycle ▀ There are 4 states that an application can be in u Active: the activity can be used by the user u Paused: The activity is partially obscured (a new non-full screen or transparent activity is active) u Stopped: The activity is totally obscured (a new full screen activity is active) u Finished: the activity has been closed.
63
Copyright 2015 by Janson Industries 63 Application Life Cycle ▀ Based on changes in the application’s “state”, several other methods will be called : u protected void onStart(){} u protected void onRestart(){} u protected void onResume(){} u protected void onPause(){} u protected void onStop(){} u protected void onDestroy(){}
64
Copyright 2015 by Janson Industries 64 Application Life Cycle ▀ An application’s “state” can be changed by user actions like: u Starting a new application u Closing an application ▀ What the new state will be will vary by what is being run u Does new app take up the whole screen? u Is there enough MM for new app?
65
Copyright 2015 by Janson Industries 65 Application Life Cycle ▀ For example, when an app is first run it means the following methods will be run: u onCreate() u onStart() u onResume() ▀ Let’s prove it!
66
Copyright 2015 by Janson Industries 66 Application Life Cycle ▀ We’ll add the following two new methods: ▀ And change onCreate a little protected void onStart(){ super.onStart(); System.out.println("*****onStart was run"); } protected void onResume(){ super.onStart(); System.out.println("*****onResume was run"); }
67
Copyright 2015 by Janson Industries 67 Here’s all the new code
68
Copyright 2015 by Janson Industries 68 Application Life Cycle ▀ Need a new emulator with the buttons enabled ▀ Need to clone a definition u Only user defined emulators can be edited ▀ Start the AVD manager
69
Copyright 2015 by Janson Industries 69 Click Device Definitions tab then Double click Galaxy Nexus definition Edit definition, click Clone Device
70
Copyright 2015 by Janson Industries 70 ▀ Define new AVD ▀ Select the first Galaxy Nexus listed ▀ If correct one selected, RAM will be 512 ▀ Fill in rest of info
71
Copyright 2015 by Janson Industries 71 Select and Start the new emulator
72
Copyright 2015 by Janson Industries 72 Run app and LogCat shows the msgs
73
Copyright 2015 by Janson Industries 73 In emulator, click home button to stop the app Run the app again
74
Copyright 2015 by Janson Industries 74 Scroll up and notice onCreate was not run, that’s because app was already created but stopped
75
Copyright 2015 by Janson Industries 75 The Whole Thing Active Activity Started on Start on Resume on Create on Pause PausedStopped on Stop on Restart Finished on Destroy Whoa! Maybe we should go step by step
76
Copyright 2015 by Janson Industries In the Beginning 76 Active Activity Started on Start on Resume on Create ▀ When the application is first run…
77
Copyright 2015 by Janson Industries From an Active State 77 ▀ The application can go to any of the other three states… Active on Pause Paused on Stop Active on Pause Finished on Destroy Active on Pause Stopped on Stop
78
Copyright 2015 by Janson Industries From an Paused State 78 ▀ The application can go Active or Stopped states… Paused Stopped Active on Resume on Stop
79
Copyright 2015 by Janson Industries From an Stopped State 79 ▀ The application can go to Finished or Active states… Active on Restart Finished on Destroy Stopped on Start on Resume
80
Copyright 2015 by Janson Industries 80 Android App Life Cycle ▀ Why all the different methods? ▀ You might want the app to do different functions when the state is changed u When closed, free up resources u When restarted, refresh info on screen u When paused, stop playing music
81
Copyright 2015 by Janson Industries 81 Emulator Glitches ▀ When emulator run, message that says something like: u Image is used by another emulator ▀ Need to go out and delete these two files in the emulator definition ▀ Substituting for username and emulator name C:/Users/username/.android/avd/EmulatorName/cache.img C:/Users/username/.android/avd/EmulatorName/userdata-qemu.img
82
Copyright 2015 by Janson Industries 82 Emulator Glitches ▀ If it starts running slowly or if logcat isn’t working well u Not displaying msgs quickly u Not allowing msg deletions ▀ Restart Eclipse ▀ Gives Eclipse a chance to clean up internally
83
Copyright 2015 by Janson Industries 83 Emulator Glitches ▀ If you get this msg: u ADB server didn't ACK, failed to start daemon ▀ Start Task Manager and kill the adb.exe process ▀ Close and restart Eclipse
84
Copyright 2015 by Janson Industries 84 Points to Remember ▀ Java classes stored in a project's package ▀ Java class consists of global level variables and methods ▀ Java method consists of local level variables and executable statements ▀ Activity classes are executable
85
Copyright 2015 by Janson Industries 85 Points to Remember ▀ LogCat displays system and program msgs ▀ An app can be in four states u Active u Paused u Stopped u Finished ▀ Changes in state will result in different methods being executed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.