Unit 1 III
Unit 1 III
What is Android
Android is a software package and Linux based operating system for mobile devices such
as tablet computers and smartphones.
It is developed by Google and later the OHA (Open Handset Alliance). Java language is
mainly used to write the android code even though other languages can be used.
The goal of android project is to create a successful real-world product that improves the
mobile experience for end users.
Open Handset Alliance (OHA)
1) It is open-source.
2) Anyone can customize the Android Platform.
3) There are a lot of mobile applications that can be chosen by the consumer.
4) It provides many interesting features like weather details, opening screen, live RSS
(Really Simple Syndication) feeds etc.
5) It provides support for messaging services(SMS and MMS), web browser, storage
(SQLite), connectivity (GSM, CDMA, Blue Tooth, Wi-Fi etc.), media, handset layout etc.
Categories of Android applications
There are many android applications in the market. The top categories are:
1. Entertainment
2. Tools
3. Communication
4. Productivity
5. Personalization
6. Music and Audio
7. Social
8. Media and Video
9. Travel and Local etc.
History of Android
An android component is simply a piece of code that has a well defined life cycle e.g.
Activity, Receiver, Service etc.
The core building blocks or fundamental components of android are activities, views,
intents, services, content providers, fragments and AndroidManifest.xml.
Activity
A screen through which a user can interact.
An activity is a class that represents a single screen.
It is like a Frame in AWT.
Pages of application.
Contains 2 modules
1. An XML file that defines user interface of the app.
2. A JAVA file containing code to handle UI.
Starts with an intent.
An Intent is a message passing mechanism telling the android application what the user is
asking it to do.
View
A view is the UI element such as button, label, text field etc. Anything that you see is a
view.
These are placed inside the layouts.
A group of arranged views on an activity is known as Viewgroup/Layout/Container.
Intent
Intent is used to invoke components. It is mainly used to:
1. Start the service
2. Launch an activity
3. Display a web page
4. Display a list of contacts
5. Broadcast a message
6. Dial a phone call etc.
For example, you may write the following code to view the webpage.
The Android emulator is an Android Virtual Device (AVD), which represents a specific
Android device.
We can use the Android emulator as a target device to execute and test our Android
application on our PC.
Android emulator provides almost all the functionality of a real device. We can get the
incoming phone calls and text messages.
It also gives the location of the device and simulates different network speeds.
Android emulator simulates rotation and other hardware sensors. It accesses the Google
Play store, and much more.
Testing Android applications on emulator are sometimes faster and easier than doing on a
real device.
For example, we can transfer data faster to the emulator than to a real device connected
through USB.
The Android emulator comes with predefined configurations for several Android phones,
Wear OS, tablet, Android TV devices.
Internal Details of Hello Android Example
Hello Android Example(.xml)
</android.support.constraint.ConstraintLayout>
}
.java
package first.javatpoint.com.welcome;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
(2) The onCreate method is called when Activity class is first created.
It is the auto-generated file that contains IDs for all the resources of res directory.
It is generated by aapt(Android Asset Packaging Tool).
Whenever you create any component on activity_main, a corresponding ID is created in the
R.java file which can be used in the Java Source file later.
APK File
As we know the modern JVM is high performance and provides excellent memory
management. But it needs to be optimized for low-powered handheld devices as well.
The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for mobile
devices. It optimizes the virtual machine for memory, battery life and performance.
The Dex(Dalvik Executable format) compiler converts the class files into the .dex file
that run on the Dalvik VM. Multiple class files are converted into one dex file.
Compiling and packaging process from the source file:
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.
App widgets
Information widgets:
Information widgets typically display a few crucial information elements and track how
that information changes over time.
Good examples for information widgets are weather widgets, clock widgets or sports score
tracking widgets.
Touching information widgets typically launches the associated app and opens a detailed
view of the widget information.
Collection widgets
Collection widgets specialize in displaying multiple elements of the same type, such as a
collection of pictures from a gallery app, a collection of articles from a news app or a
collection of emails/messages from a communication app.
Collection widgets can scroll vertically.
Collection widgets typically focus on the following use cases:
1. Browsing the collection
2. Opening an element of the collection to its detail view in the associated app for
consumption
3. Interacting with elements such as marking them done (with new support for compound
buttons in Android 12 (API level 31))
Control widgets
The main purpose of a control widget is to display often-used functions, so that the user can
trigger right from the home screen without having to open the app first.
You can think of them as remote controls for an app.
An example of a control widget is a home control widget that lets users turn lights on or off
in different rooms of a house.
Hybrid widgets
While some widgets tend to gravitate towards one of the types in the preceding sections
(information, collection, or control), many widgets in reality are hybrids that combine
elements of different types.
A music player widget is primarily a control widget, but also keeps the user informed about
what track is currently playing.
It essentially combines a control widget with elements of an information widget type.
When planning your widget, design around one of the base types and add elements of other
types if needed.
Design guidelines
Widget content
Widgets are a great mechanism to attract a user to your app by "advertising" new and
interesting content that is available for consumption in your app.
Just like the teasers on the front page of a newspaper, widgets should consolidate and
concentrate an app's information and then provide a connection to richer detail within the
app; or in other words: the widget is the information "snack" while the app is the "meal."
As a bottom line, always make sure that your app shows more detail about an information
item than what the widget already displays.
Widget navigation
Besides the pure information content, consider rounding out your widget's offering by
providing navigation links to frequently used areas of your app.
This lets users complete tasks quicker and extends the functional reach of the app to the
home screen.
Widget resizing
Long-pressing on a resizable widget, and subsequently releasing puts that widget into
resize mode.
Users can use the drag handles or the widget corners to set the desired size.
Toasts overview
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
.java
package example.javatpoint.com.activitylifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy invoked");
}
}
logcat: onCreate, onStart and onResume methods are
invoked.
HOME Button. You will see onPause method is invoked.
After a while, you will see onStop method is
invoked.
Android Intent
Android Intent is the message that is passed between components such as activities,
content providers, broadcast receivers, services etc.
It is generally used with startActivity() method to invoke activity, broadcast receivers etc.
It can be described as the intention to do action.
Used for communication between system and application, multiple activities.
Android intents are mainly used to:
Start the service
Launch an activity
Display a web page
Display a list of contacts
Broadcast a message
Dial a phone call etc.
Types of Android Intents
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
Button button;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url=editText.getText().toString();
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
}
}
Android Explicit Intent
Android Explicit intent specifies the component to be invoked from activity. In other
words, we can call another activity in android by explicit intent.
We can also pass the information from one activity to another using explicit intent.
Here, we are going to see an example to call one activity from another and vice-versa.
Android calling one activity from
another activity example
Fundamentals of testing
Android apps
Benefits of testing
Subject
There are different types of tests depending on the subject:
Functional testing: does my app do what it's supposed to?
Performance testing: does it do it quickly and efficiently?
Accessibility testing: does it work well with accessibility services?
Compatibility testing: does it work well on every device and API level?
Scope
Tests also vary depending on size, or degree of isolation:
Unit tests or small tests only verify a very small portion of the app, such as a method or
class.
End-to-end tests or big tests verify larger parts of the app at the same time, such as a
whole screen or user flow.
Medium tests are in between and check the integration between two or more units.
Instrumented versus local tests
Instrumented tests run on an Android device, either physical or emulated.
The app is built and installed alongside a test app that injects commands and reads the
state. Instrumented tests are usually UI tests, launching an app and then interacting with it.
Local tests execute on your development machine or a server, so they're also
called host-side tests. They're usually small and fast, isolating the subject under test from
the rest of the app.
Debug your app
Android Studio provides a debugger that allows you to do the following and more:
Select a device to debug your app on.
Set breakpoints in your Java, Kotlin, and C/C++ code.
Examine variables and evaluate expressions at runtime.