Android Development
Android Development
- Good performance
- Platform independent
- Effectiveness (a lot of powerful features)
- Excellent documentation
- Excellent and free tools
- Object-oriented
Object-oriented Programming
public Car() {
tires = 4;
licensePlate = “ABC-123”;
In this example: }
Return type = void
public void removeTires(int amount) {
Parameters = amount tires = tires - amount;
}
}
Constructor
- Define who can access the method or field public class Car {
- Public: Everyone private int tires;
- Private: only within the same object private String licensePlate;
- Protected: only within same object or
public Car() {
subclasses tires = 4;
licensePlate = “ABC-123”;
}
private tires;
private String licensePlate;
Small test private String carColor;
int result = 0;
- Example: result = 7 - 2;
+ Example: result = 7 + 2;
/ Example: result = 7 / 2;
* Example: result = 7 * 2;
% Example: result = 7 % 2;
Shorthand notation:
result += 2;
result += 2;
result /= 2;
result *= 2;
result %= 2;
Unary Operators
int result = 0;
- Example: result = result - 2;
+ Example: result = result + 2;
-- Example: result--;
++ Example: result++;
String something;
something += “Hello”;
something += “ world!”;
And this:
something += 7;
Or this:
What if you want code to run only when a public class Car {
condition is met? ...
- Hardware:
- CPU
- RAM
- GPU
- Storage
- Battery
Android devices, small computers
Android devices, small computers
- Software:
- Let’s have a look!
Android Update
Adoption
● Data from 17/01/2018
● Marshmallow > Nougat > Lollipop
● Oreo: 0.7% !
● Applications
● Application framework
● Libraries
● Android runtime
● Linux Kernel
Applications
● The Android runtime comes with a set of core libraries that implement
most of the Java programming language. Each Android app runs in its own
process.
● In android runtime, there are core libraries and DVM (Dalvik Virtual
Machine) which is responsible to run android application. DVM is like JVM
but it is optimized for mobile devices. It consumes less memory and
provides fast performance.
Android Emulator
● Android Emulator is used to run, debug and test the android application. If
you don't have the real device, it can be the best way to run, debug and test
the application.
● It uses an open source processor emulator technology called QEMU.
Dalvik Virtual Machine | DVM
● The javac tool compiles the java source file into the class file.
● The dx tool takes all the class files of your application and generates a
single .dex file. It is a platform-specific tool.
● The Android Assets Packaging Tool (aapt) handles the packaging process.
Linux Kernel
● Underneath everything else lies the Linux kernel. Android relies on the
kernel for drivers, and also core services such as security and memory
management.
Libraries/Middleware
● Layouts only define the appearance of the app. You define what the app
does by writing Java code. A special Java class called an activity decides
which layout to use and tells the app how to respond to the user. As an
example, if a layout includes a button, you need to write Java code in the
activity to define what the button should do when you press it.
● Layouts tell Android what the screens in your app look like and Activities
define what the app should do.
Cont..
● In addition to Java code and layouts, Android apps often need extra
resources such as image files and application data. You can add any extra
files you need to the app. Android apps are really just a bunch of files in
particular directories. When you build your app, all of these files get
bundled together, giving you an app you can run on your device.
Android Updates: Project Treble
Android devices, small computers
The future:
- More processing power
- Smaller
- Cheaper, maybe
Demo: Android Studio
Assignment for tomorrow: revise slides of today, install Android Studio and successfully run a
template project.
- Source files
- Resources
- Drawables
- Layouts
- Mipmaps
- Values
- Menu
- Animations
- Colors
- Fonts
Resources: drawables
- Filetype: XML
- Key-value pairs
- Important: id
- Anything is possible
Resources: layouts
- Filetype: XML
- Key-value pairs
- Important: id
- Anything is possible
Resources: values
- Filetype: XML
- Example: store predefined
Strings
- Why useful:
- Easy translation
- Easy reuse
- Clean code
Lifecycle
Lifecycle: remember
Lifecycle
→ onClick listener
- Listens all the time until the user presses the button
- Run some code when the click is detected
Activities: interaction
AND
Activities: navigation
Intents
Cont..
- Implicit
- Android device knows how to handle the
intent, you don’t tell the device what to do
- Example: opening a webpage
- Explicit
- You tell the device what to do
- Example: starting a new activity
Activities: implicit intent
Activities: explicit intent
Activities: intents
In code:
- Explicit intent
- Implicit intent
RecyclerView
● View
A view is the UI element such as button, label, text field etc. Anything that you
see is a view.
- We want to display a very long list of items
- Problem: a lot of memory needed
- Solution: RecyclerView
- Only keeps in memory what is on the screen + some items before and some
after
- Needs an adapter to handle the data
RecyclerView
RecyclerView: adapter
● Content Provider
● Content Providers are used to share data between the applications.
● Fragment
● Fragments are like parts of activity. An activity can display one or more
fragments on the screen at the same time.
● AndroidManifest.xml
● It contains informations about activities, content providers, permissions
etc. It is like the web.xml file in Java EE.
AndroidManifest.xml file in android
○ It is responsible to protect the application to access any protected parts by providing the
permissions.
○ It lists the instrumentation classes. The instrumentation classes provides profiling and
other informations. These informations are removed just before the application is
published etc.
Option menu
- OnSaveInstanceState
- Shared Preferences
- Internal Storage
- External Storage
- SQLite Database
- Network Connection
Storage: onSaveInstanceState
Source: https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en
Storage: Shared Preferences
- Stored on device
- Public or private
- Key-value pairs
- Read & write
Storage: Shared Preferences
Room demo
Storage: Room database
Summary:
- Uses SQLite database
- Reduces code needed
- Limited (for now)
Server
● Why multithreading?
○ Used to divide download/calculation load between threads
See developer.android.com/guide/topics/ui/notifiers/notifications.html
for more information
Set a notification channel
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel("my_channel_01",
"Channel1",NotificationManager.IMPORTANCE_LOW);
// Configure the notification channel.
mChannel.setDescription("These are the main notifications");
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setLightColor(Color.RED);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
Showing a basic notification