Download presentation
Presentation is loading. Please wait.
1
Android Programming Tutorial
2019/2/19
2
Introduction to Android
Reference 01 Introduction to Android 02 Android environment set up 03 Android project layout 04 Application components 05 Examples 06
3
1. Reference Online guide Book
You may need VPN to visit this website Book Android Tutorial (by tutorialspoint.com) Android – a programmers guide (J.F. DiMarzio) Andbook (Nicolas Gramlich)
4
2. Introduction to Android
What is Android A linux-based OS for mobile devices Developed by Google First Android phone in October 2008 Many companies produce Android phones(Samsung, Motorola, LG…) Android logo Android phone
5
Introduction to Android
Why android? The first truly open and comprehensive platform for mobile devices No licensing, distribution, or development fees Development over many platform How does Android manage Applications? Multiple applications can run at same time Only one instance of an App runs at a time Typical buttons: Home, Back, Menu, Search
6
3. Android environment set up
2GB RAM and 2GB disk space JDK + Android Studio on your own computer Guide is here:
7
Concepts JDK Java Development Kit
Provide a Java Virtual Machine to run java code Includes a complete JRE plus tools for developing, debugging, and monitoring Java applications. Android is based on java
8
Concepts Android Studio Android SDK
Android Studio provides the fastest tools for building apps on every type of Android device. Android SDK The bulk of the Android SDK, in number of files, consists of documentation, with programming APIs, tools, and samples comprising the rest.
9
5. Application Components
Activity An activity represents a single screen with a user interface. Service (Background) E.g. Network communication Intent Inter-communication among activities or services Notification Signaling users: light, sound, icon, dialog E.g. new message arrives
10
Activities They dictate the UI and handle the user interaction to the smartphone screen Life of Activity If you have worked with C, C++ or Java programming language then you must have seen that your program starts from main() function. Very similar way, Android system initiates its program with in an Activity starting with a call on onCreate() callback method. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity as shown in the below Activity life cycle diagram:
11
Activities Callback Description onCreate() This is the first callback and called when the activity is first created. onStart() This callback is called when the activity becomes visible to the user. onResume() This is called when the user starts interacting with the application onPause() The paused activity does not receive user input and cannot execute any code and called when the current activity is being paused and the previous activity is being resumed.
12
Activities Callback Description onStop() This callback is called when the activity is no longer visible. onDestroy() This callback is called before the activity is destroyed by the system. onRestart() This callback is called when the activity restarts after stopping it.
13
Activities – java code
14
Intents An Android Intent is an object carrying an intent ie. message from one component to another component with-in the application or outside the application We can start a new activity using Intents. An Android Intent is an abstract description of an operation to be performed.
15
Examples – “Hello World”
Several steps: (1) Start Android Studio and New project (2) New activity (3) Design GUI layout (4) Write java code for the activity (5) Run the activity on the virtual machine or your own mobile phone
16
(1) New project File -> New Project
17
New project
18
(2) New activity
19
New activity
20
4. Android Studio 1 2 3 1 project files editor debug information
21
Project architecture Src:
This contains the .java source files for your project. By default, it includes an mainActivity.javasource file having an activity class that runs when your app is launched using the app icon.
22
Project architecture 3. Res/layout
This is a directory for files that define your app's user interface.
23
(3) GUI design MyActivity.java corresponds to activity_my.xml
24
GUI design
25
Layout and views
26
(5) Run it in a virtual device
1 2
27
Create a virtual device
Set device name, type, android version, CPU type, memory size. Android version of device should be higher than that of project. RAM size should not be very large, otherwise your computer will be very slow
28
Start the virtual device
29
Start the virtual device
30
Run app on the virtual device
Choose the virtual device
31
Run the application on your physical device
You may need to install driver for your phone Connect your phone to the computer Turn on the developer options -> USB debugging mode Run your application in Android Studio, and choose your phone as the device
32
Run the application on your physical device
developer options -> USB debugging mode
33
“Hello world~”
34
6.2 examples - Add a Button For the “hello world ” application, there is just a TextView. We can add more Views and controls(e.g. Button) We should tell the application what to do, when user click the button.
35
Steps Add Button View in the layout Set the properties of Button
Write java code in the src/*.java Run the application
36
Add Button, set property
37
Properties
38
Write java code Two variables Get views Set click listener Change text
39
Run application Click the button
40
6.3 examples - Start a new activity
New the second activity Edit the layout files Write java code Run the application
41
New the second activity
42
New the second activity
43
Activity layout Activity 1 Activity 2
44
Write java code Button variables Get views Set click listener
Change text This is the MainActivity class I didn’t modify Test1 class
45
Start a new activity Click “Start Activity”
46
6.4 examples - Sensor 1. Motion Sensors 2. Position Sensors
Accelerometer (also: Gravity, Linear Accl) Gyroscope Rotation Vector 2. Position Sensors Magnetic Field Orientation Proximity 3. Environmental Sensors Temperature, Light, Pressure, Humidity 4. Others GPS, Camera, Microphone, Network
47
Steps for getting sensor values
Get a sensor manager Get sensor list with sensor manager Register listener with sensor manager Implement function “onSensorChanged” in a listener Get values of sensors in the function
48
Java codes SensorManager manager = (SensorManager) getSystemService( Context.SENSOR_SERVICE ); Sensor light = manager.getDefaultSensor( Sensor.TYPE_LIGHT ); manager.registerListener( this, light, SensorManager.SENSOR_DELAY_NORMAL); public void onSensorChanged( SensorEvent event ) { float lux = event.values[0]; }
49
Show sensor values in a TextView
This class should implements “SensorEventListener”
50
Result
51
6.5 examples - ListView ListView is a view group that displays a list of scrollable items Put ListView in the activity layout Define layout for each item of ListView Write code
52
Put a ListView in the screen
53
Layout for each item Define a layout for each item in the ListView
Right click ->new ->XML ->layout XML File
54
Layout for each item
55
ListView -- java code Get data for display Get ListView New an adapter
Connect data and item layout with adapter ListView set adapter
56
ListView – java code Prepare data Set Adapter Set adapter
57
ListView -- “getData()”
Data format Key: value
58
ListView result
59
log The application programmer can write custom log messages.
Log.v(); // Verbose Log.d(); // Debug Log.i(); // Info Log.w(); // Warning Log.e(); // Error This is very useful for debugging
60
Tips Alt + Enter : for importing package or fixing your code
Another way to install application in your mobile phone. Find “ [your project folder] /app/build/outputs/app/*.apk ” file Copy the file to your phone Install the apk
61
Project requirements Show sensors on your mobile phone
Show the values of these sensors Try your best to make the user interface beautiful and friendly Do not copy codes of others
62
Thank You!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.