Академический Документы
Профессиональный Документы
Культура Документы
Jehad Aldahdooh
Faculty of Engineering Mobile Computing
Computer Engineering Dept. Android Lab
1. Open Eclipse.
1
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
3. Name the project : In this stage, as shown below there are exist three names described as:
Application Name
Shown in Play Store and Settings .Usually same as Project Name.
Project Name
Eclipse project name. Follow naming convention you use for Eclipse. Not used
elsewhere.
Package name
Apps on a particular Android device must have unique packages, so use
com.yourCompany.project
2
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
Target Build SDK
The Android version used to build/compile your project. This can be any version,
but the safest option is to make it match the minimum SDK below.
4. Click Next
Defaults
Use defaults for development and testing. Just press Next.
3
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
Activity Name
Name of main Java class. This is the class you will edit first. Class name often
corresponds to project name.
Layout Name
Base name of XML file in res/layout folder. Used to give layout to app. often
just called main. Will be referred to in main Java class with
R.layout.layout_name.
4
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
5
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
Source Code
6
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
Before we can run the application, we need to setup an Android Vitual Device(AVD), or
emulator, to run it on:
Select the menu Window -> "Android SDK and AVD Manager".
Select the target build that we would like to run the application on
7
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
Project Modifications
From the Package Explorer, double-click the file AndroidManifest.xml.
Phone Modifications
o Select Settings -> Applications -> Development.
Ensure the device is properly connected. Run the application as you would normally.
8
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
1. Java-based
Use Java to define Strings, lay out window, create GUI controls, and assign event
handlers. Like Swing programming.
1. XML-based
Use XML files to define Strings, lay out window, create GUI controls, and assign
event handlers. The Java method will read the layout from XML file and pass it to
setContentView.
2. Hybrid
Use an XML file to define Strings, layout window and create GUI controls. Use Java
to assign event handlers.
Advantages
Familiar to Java desktop developers. Like approach used for Swing, SWT, and
AWT.
Good for layouts that are dynamic (i.e., that change based on program logic).
Disadvantages
Harder to maintain.
Not generally recommended except for dynamic layouts.
But still acceptable for App Store. Whatever works best for your programmers
and your app. No code police.
9
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
OnClickListener is a public inner class inside View. But, as long as you import
android.view.View.OnClickListener, you use it just like a normal class. And, remember that
Eclipse helps you with imports: just type in the class name.
10
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
Disadvantages
Works poorly for dynamic layouts
11
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
Activity Class
Activity: Much like a Form for a web page, activities display a user interface for the
purpose of performing a single task. An example of an Activity class would be one which
displays a Login Screen to the user.
o This method will be called when the Activity starts and is where initialization of local
and member data will be done.
12
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
o make a call to super.onCreate(savedInstanceState)
This should always be done and is to ensure that any necessary parent class
initializations are performed.
When you created the XML layout file earlier, the Android Eclipse Plugin
automatically added a static constant to the static R.layout class in the R.java
file under the /gen folder. This constant variable has the same name of the file
and it's value is used to identify the layout file.
This call tells Android to create a screen based off of the layout file.
Implement the OnClickListener interface by creating a method stub with the following
signature: public void onClick(View v) .
Android Lifecycle
The Activity base class defines a series of events that govern the life cycle of an
activity. The Activity class defines the following events:
onCreate() Called when the activity is first created
onStart() Called when the activity becomes visible to the user
onResume() Called when the activity starts interacting with the user
onPause() Called when the current activity is being paused and the previous
activity is
being resumed
onStop() Called when the activity is no longer visible to the user
onDestroy() Called before the activity is destroyed by the system (either
manually or by the system to conserve memory
onRestart() Called when the activity has been stopped and is restarting again
13
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
14
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
15
Lab 1: Getting Started With Android Programming Mobile Computing - Android
Lab
3. If you click the Back button on the Android emulator, the following is printed:
In the onPause() event
In the onStop() event
In the onDestroy() event
4. Click the Home button and hold it there. Click the Activities icon and observe the following:
In the onCreate() event
In the onStart() event
In the onResume() event
5. Click the Phone button on the Android emulator so that the activity is pushed to the
background.Observe the output in the LogCat window:
In the onPause() event
In the onStop() event
6. Notice that the onDestroy() event is not called, indicating that the activity is still in memory.
Exit the phone dialer by clicking the Back button. The activity is now visible again. Observe the
output in the LogCat window:
In the onRestart() event
In the onStart() event
In the onResume() event
16