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
Lecture Series on Android Programming Chapter.2 First Android Application Lecturer: Prof.Luqun Li ([email protected]) Teaching Assistants: Fengyou Sun, Haijun Yang, Ting Sun Contents 2 1 Set up the environment 2 Learing the Fundamental Components 3 Hello world 4 Exploring the Structure of an Android Applica Shanghai Normal University Introducing Andorid computing platform 3 5 Analyzing the Stucture of an Andorid Applicat 6 Analyzing the Notepad Application 7 Examining the Applicationg Lifecycle 8 Debugging the App Shanghai Normal University Setting Up Your Environment To build Android applications, we need to establish a development environment. In Prepare the softwar e bundles Java JDK & Java Doc (optional) IDE Environment can be setup by: Approach.1 Use:Android SDK + ADT+Eclipes Approach.2 Use:Android SDK+MotoDEV studio See videos for: android related software's download & IDE setup 4 Shanghai Normal University Aproach.1 Eclipse+Android SDK+ADT step1 Downloading JDK 7 and Eclipse 4.2 5 Shanghai Normal University Click to edit headline step2 Downloading the Android SDK 6 Shanghai Normal University Click to edit headline step3 Installing Android Development Tools (ADT) 7 Shanghai Normal University Learning the Fundamental Components Every application framework has some key components that developers need to understand before they can begin to write applications based on the framework 8 Shanghai Normal University The Fundamental Components View Activity Intent Content Provider Service AndroidManifest.xm 9 Shanghai Normal University A Hello World Demo Hello World! Now we ready to build our first Andr oid application. We’ll start by buildin g a simple “Hello World!” program. Create the sk eleton of the application by followin g these steps: 10 Shanghai Normal University Click to edit headline step 1 Launch Eclipse and select File ➤ New ➤ Project. In the “New Project” dialog box, select “Android” and then click “Next.” You will then see the “New Android Project” dialog box 11 Shanghai Normal University Click to edit headline step 2 enter HelloAndroid as the project name, pro. android asthe package name, HelloActivity as the activity name, and HelloAndroidApp as the application name 12 Shanghai Normal University Click to edit headline step 3 Click the “Finish” button, which tells ADT to generate the project skeleton for you.For now, open the HelloActivity.java file under he src folder and modify the onCreate() method as follows 13 Shanghai Normal University /** Called when the activity is first created. * / @Override public void onCreate(Bundle savedInstanceStat e) { super.onCreate(savedInstanceState); /** create a TextView and write Hello World! */ TextView tv = new TextView(this); tv.setText ("Hello World!"); /** set the content view to the TextView */ setContentView(tv); } 14 Shanghai Normal University create an Eclipse launch configuration Click the “Browse…” button and select the HelloAndroid project Under “Launch Action,” select “Launch” and select “pro.android.HelloActivity” from the drop-down list Click “Apply” and then “Run.” You should see the emulator launched with the Hello Android projec. Next page 15 Shanghai Normal University create an Eclipse launch configuration Select Run ➤ Run Configurations In the “Run Configurations” dialog box, double-click “Android Application" in the left pane. The wizard will insert a new configuration named “New Configuration.” Rename the configuration RunHelloWorld. Create the Eclipse launch configuration 16 Shanghai Normal University Exploring the Structure of an Androi d Application Although the size and complexity of Android appli cations can vary greatly, their structures will be similar An Android application is primarily ma de up of three pieces 17 Shanghai Normal University Click to edit headline an Android application the application descriptor 18 a collection of vari the application’ ous resources s source code. Shanghai Normal University Analyzing the Notepad Application analyzing its components will give you some realistic insight into Android development. Follow these steps to load the Notepad sa mple into the Eclipse IDE: 1. Start Eclipse. 2. Go to File ➤ New ➤ Project. 3. In the “New Project” dialog, select An droid ➤ Android Project. 19 Shanghai Normal University 4.In the “New Android Project” dialog, se lect “Create project from existing source” and set the “Location” field to the path of the Notepad application. Note that the N otepad application is located in c:\Androi dSDK\samples\, which you downloaded e arlier. After you set the path, the dialog reads the AndroidManifest.xml file and p repopulates the remaining fields in the “New Android Project” dialog box. 5. Click the “Finish” button. 20 Shanghai Normal University Next, we see the activity execute a mana ged query and get a cursor for the result. A managed query means that Android wil l manage the returned cursor. In other w ords, if the application has to be unloade d or reloaded, neither the application nor the activity has to worry about positionin g the cursor, loading it, or unloading it. T he parameters to managedQuery(), show n in Table , are interesting 21 Shanghai Normal University Parameters to Activity.managedQuery 22 Parameter Data Type Description URI Uri projection String[] URI of the conten t provider The column to ret urn (column name s) selection String Optional where cl ause selectionArgs String[] The arguments t o the selection, i f the query contai ns sortOrder String SortShanghai order Normal to be University u Examining the Application Lifecycle The lifecycle of Android applications differs great ly from the lifecycle of web-based J2EE applicat ions. J2EE apps are loosely managed by the con tainer they run in. For example, a J2EE contain er can remove an application from memory if it sits idle for a predetermined time period. But th e container generally won’t move applications i n and out of memory based on load and/or avai lable resources. In other words, it’s up to the ap plication owners to ensure that resources are av ailable. 23 Shanghai Normal University The concept of application lifecycle is logical, bu t a fundamental aspect of Android applications c omplicates matters. Specifically, the Android ap plication architecture is compo- nent- and integ ration-oriented. This allows a rich user experien ce, seamless reuse, and easy application integr ation, but creates a complex task for the applica tion-lifecycle manager. 24 Shanghai Normal University The following graph shows the list of lifecycle met hods that Android calls during the life of an acti v- ity. It’s important to understand when each of the methods is called by the system to ensur e that you implement a stable application. Note that you do not need to react to all of these met hods. If you do, however, be sure to call the sup erclass versions as well. 25 Shanghai Normal University onRestart Activity Start onCreate onStop onStar r onResume 26 onDestroy Activity Stop onPause Shanghai Normal University Debugging Your App After we write a few lines of code for our first application, w e’ll start wondering if it’s pos- sible to have a debug sessi on while you interact with your application in the emulator. Shortly after that, you’ll instinctively run to System.out.p rintln(), which will fail because the code is running on the emulator and the sys-out statement is not fed back to th e IDE. But don’t worry; the Android SDK includes a host of applications that we can use for debugging purposes. The SDK also includes a file-explorer tool that you can us e to view files on the device. These tools are integrated with the Eclipse IDE 27 Shanghai Normal University Click to edit headline You can view the tools b y selecting the Debug per spective in Eclipse. You c an also launch each tool by going to Window ➤ Sh ow View ➤ Other ➤ Andr oid. One of the tools that you’l l use throughout your And roid development is LogC at. This tool displays the l og messages that you em it using android.util.Log, exceptions, and so on. W e will introduce the other tools throughout the book.t. 28 Shanghai Normal University Summary This chapter showed: 1. 2. 3. 4. 29 how to set up IDE discussed & introduced views, activities, i ntents, content providers, and services. Talk about the Android application lifecycle. Finally, mentioned the Android SDK’s debug ging tools Shanghai Normal University