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