290 likes | 426 Views
Chapter.2 First Android Application. Lecturer: Prof.Luqun Li ( liluqun@gmail.com ) Teaching Assistants: Fengyou Sun, Haijun Yang, Ting Sun. 1. 2. 3. 4. Set up the environment. Learing the Fundamental Components. Hello world. Exploring the Structure of an Android Application. Contents.
E N D
Chapter.2 First Android Application Lecturer: Prof.Luqun Li (liluqun@gmail.com) Teaching Assistants: Fengyou Sun, Haijun Yang, Ting Sun
1 2 3 4 Set up the environment Learing the Fundamental Components Hello world Exploring the Structure of an Android Application Contents
5 6 7 8 Analyzing the Stucture of an Andorid Application Analyzing the Notepad Application Examining the Applicationg Lifecycle Debugging the App Introducing Andorid computing platform
Setting Up Your Environment To build Android applications, we need to establish a development environment. In Prepare the software 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
Aproach.1 Eclipse+Android SDK+ADT step1 Downloading JDK 7 and Eclipse 4.2
Click to edit headline step2 Downloading the Android SDK
Click to edit headline step3 Installing Android Development Tools (ADT)
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
The Fundamental Components View Activity Intent Content Provider Service AndroidManifest.xm
A Hello World Demo • Hello World! Now we ready to build our first Android application. We’ll start by building a simple “Hello World!” program. Create the skeleton of the application by following these steps:
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
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
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 underhe src folder and modify the onCreate() method as follows
/** Called when the activity is first created. */ • @Override • public void onCreate(Bundle savedInstanceState) { • 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); • }
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 emulatorlaunched with theHelloAndroid projec. Next page
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
Exploring the Structure of an Android Application Although the size and complexity of Android applications can vary greatly, their structures will be similar An Android application is primarily made up of three pieces
an Android application Click to edit headline the application descriptor a collection of various resources the application’s source code.
Analyzing the Notepad Application analyzing its components will give you some realistic insight into Android development. Follow these steps to load the Notepad sample into the Eclipse IDE: • 1. Start Eclipse. • 2. Go to File ➤ New ➤ Project. • 3. In the “New Project” dialog, select Android ➤ Android Project.
4.In the “New Android Project” dialog, select “Create project from existing source” and set the “Location” field to the path of the Notepad application. Note that the Notepad application is located in c:\AndroidSDK\samples\, which you downloaded earlier. After you set the path, the dialog reads the AndroidManifest.xml file and prepopulates the remaining fields in the “New Android Project” dialog box. • 5. Click the “Finish” button.
Next, we see the activity execute a managed query and get a cursor for the result. A managed query means that Android will manage the returned cursor. In other words, if the application has to be unloaded or reloaded, neither the application nor the activity has to worry about positioning the cursor, loading it, or unloading it. The parameters to managedQuery(), shown in Table , are interesting
Examining the Application Lifecycle The lifecycle of Android applications differs greatly from the lifecycle of web-based J2EE applications. J2EE apps are loosely managed by the container they run in. For example, a J2EE container can remove an application from memory if it sits idle for a predetermined time period. But the container generally won’t move applications in and out of memory based on load and/or available resources. In other words, it’s up to the application owners to ensure that resources are available.
The concept of application lifecycle is logical, but a fundamental aspect of Android applications complicates matters. Specifically, the Android application architecture is compo- nent- and integration-oriented. This allows a rich user experience, seamless reuse, and easy application integration, but creates a complex task for the application-lifecycle manager.
The following graph shows the list of lifecycle methods that Android calls during the life of an activ- ity. It’s important to understand when each of the methods is called by the system to ensure that you implement a stable application. Note that you do not need to react to all of these methods. If you do, however, be sure to call the superclass versions as well.
onRestart Activity Start onDestroy onStop onCreate onStar r Activity Stop onResume onPause
Debugging Your App After we write a few lines of code for our first application, we’ll start wondering if it’s pos- sible to have a debug session while you interact with your application in the emulator. Shortly after that, you’ll instinctively run to System.out.println(), which will fail because the code is running on the emulator and the sys-out statement is not fed back to the 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 use to view files on the device. These tools are integrated with the Eclipse IDE
Click to edit headline • You can view the tools by selecting the Debug perspective in Eclipse. You can also launch each tool by going to Window ➤ Show View ➤ Other ➤ Android. One of the tools that you’ll use throughout your Android development is LogCat. This tool displays the log messages that you emit using android.util.Log, exceptions, and so on. We will introduce the other tools throughout the book.t.
Summary This chapter showed: • how to set up IDE • discussed & introduced views, activities, intents, content providers, and services. • Talk about the Android application lifecycle. • Finally, mentioned the Android SDK’s debugging tools