Mobile Computing Answer
Mobile Computing Answer
1) Linux kernel
It is the heart of android architecture that exists at the root of
android architecture. Linux kernel is responsible for device
drivers, power management, memory management, device
management and resource access.
2) Native Libraries
On the top of linux kernel, their are Native libraries such as
WebKit, OpenGL, FreeType, SQLite, Media, C runtime library
(libc) etc.
The WebKit library is responsible for browser support, SQLite is
for database, FreeType for font support, Media for playing and
recording audio and video formats.
3) Android Runtime
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.
4) Android Framework
On the top of Native libraries and android runtime, there is
android framework. Android framework includes Android
API's such as UI (User Interface), telephony, resources,
locations, Content Providers (data) and package managers. It
provides a lot of classes and interfaces for android application
development.
5) Applications
On the top of android framework, there are applications. All
applications such as home, contact, settings, games, browsers
are using android framework that uses android runtime and
libraries. Android runtime and native libraries are using
linuxkernal.
@Override
protected void onCreate(Bundle savedInstanceState) {
EditTexteditText;
Button button;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn);
editText = (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);
}
});
}
}
Explicit Intent
Using explicit intent any other component can be specified. In
other words, the targeted component is specified by explicit
intent. So only the specified target component will be
invoked. For Example:
Explicit Intent Example
In the above example, There are two activities (FirstActivity,
and SecondActivity). When you click on the ‘GO TO OTHER
ACTIVITY’ button in the first activity, then you move to the
second activity. When you click on the ‘GO TO HOME ACTIVITY’
button in the second activity, then you move to the first
activity. This is getting done through Explicit Intent.
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
publicclassMainActivityextendsAppCompatActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu){
MenuInflaterinflater=getMenuInflater();
inflater.inflate(R.menu.menu_example, menu);
returntrue;
}
@Override
publicbooleanonOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
caseR.id.action_settings:
// Handle settings action
returntrue;
caseR.id.action_about:
// Handle about action
returntrue;
default:
returnsuper.onOptionsItemSelected(item);
}
}
}
3. Handle Menu Item Selections
In the onOptionsItemSelected method, you can handle the
actions when a menu item is selected. In the example above,
you can add the logic for what happens when the user selects
"Settings" or "About".
Summary
By following these steps, you can create a functional Options
Menu in your Android application. The menu is defined in an
XML file for better organization and separation of concerns,
making it easier to manage and modify. This approach
enhances the user experience by providing a familiar interface
for accessing app functions and settings.
Q-3)B) Write a short note on "adaptive layout "
Adaptive layouts in Android are designed to provide a seamless
user experience across a wide range of device sizes and
configurations, from smartphones to large tablets and foldable
devices. The goal of adaptive layouts is to adjust the user
interface dynamically based on the available screen space,
ensuring that the app remains usable and visually appealing
regardless of the device.
Key Features of Adaptive Layouts
1. Responsive Design: Adaptive layouts utilize responsive
design principles, allowing UI elements to resize and rearrange
themselves based on the screen dimensions. This is essential
for accommodating different orientations and screen sizes.
2. Window Size Classes: Android introduces the concept of
window size classes, which categorize devices into compact,
medium, and expanded sizes. This classification helps
developers determine how to adjust their layouts effectively
based on the available space.
3. Use of Jetpack Compose: With Jetpack Compose, developers
can create adaptive layouts more easily. The framework
provides composable functions that help manage different
layouts based on the window size classes, streamlining the
process of creating responsive UIs.
4. Alternative Layout Resources: Developers can create
alternative layouts for different screen configurations by using
resource qualifiers. This allows for specific layouts to be loaded
based on the device's characteristics, enhancing the
adaptability of the app.
5. Multi-Window Support: Adaptive layouts also consider multi-
window modes, allowing apps to adjust their UI when used
alongside other applications. This is particularly relevant for
foldable devices and larger screens, where users may run
multiple apps simultaneously.
Example of Adaptive Layout Implementation
Here's a brief example of how you might implement an
adaptive layout using Jetpack Compose:
kotlin
@Composable
funMyAdaptiveLayout(){
valwindowSizeClass=calculateWindowSizeClass()// Get the
current window size class
when(windowSizeClass.widthSizeClass){
WindowWidthSizeClass.Compact->{
// Layout for compact screens (e.g., phones)
Column {
Text("Compact Layout")
// Add other UI elements
}
}
WindowWidthSizeClass.Medium->{
// Layout for medium screens (e.g., small tablets)
Row {
Text("Medium Layout")
// Add other UI elements
}
}
WindowWidthSizeClass.Expanded->{
// Layout for expanded screens (e.g., large tablets)
Row {
Text("Expanded Layout")
// Add other UI elements
}
}
}
}
In this example, the layout adapts based on the current window
size class, allowing for different arrangements of UI elements
depending on the device's screen size.
@Override
publicvoidonCreate(){
super.onCreate();
// Initialization code
}
@Override
publicintonStartCommand(Intent intent,int flags,intstartId){
// Code to perform background work
return START_STICKY;// Indicates the service should be
restarted if killed
}
@Override
publicIBinderonBind(Intent intent){
returnnull;// Not a bound service
}
@Override
publicvoidonDestroy(){
super.onDestroy();
// Cleanup code
}
}
Conclusion
Services in Android are powerful components that enable
background processing without user interaction. Understanding
the service lifecycle and how to manage it effectively is crucial
for developing responsive and efficient applications. By
leveraging started and bound services, developers can create
applications that perform tasks seamlessly in the background.
Category Description
Class Description
public
class MainActivity extends AppCompatActivity implements Sens
orEventListener {
private SensorManager mgr;
private Sensor sensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mgr =
(SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensor = mgr.getDefaultSensor(Sensor.TYPE_LIGHT);
List<Sensor>deviceSensors
= mgr.getSensorList(Sensor.TYPE_ALL);
}
@Override
public final void onAccuracyChanged(Sensor
sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
@Override
public final void onSensorChanged(SensorEvent event) {
// Do something with this sensor value.
}
@Override
protected void onResume() {
super.onResume();
mgr.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
MainActivity.java
package
in.edu.vpt.insertusingasyn
c; import
android.app.ProgressDialo
g; import
android.content.Context;
import
android.database.sqlite.SQLiteDat
abase; import android.os.Bundle;
import
android.support.v7.app.AppCompatA
ctivity; import android.view.View;
import android.widget.Button;;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState
);
setContentView(R.layout.activity_m
ain);
createData =
(Button)findViewById(R.id.button);
createData.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
sqLiteDatabaseObj =
openOrCreateDatabase("AndroidJSonDataBase",
Context.MODE_PRIVATE, null);
} });
}
Geocoder:
A class for handling geocoding and reverse geocoding.
Geocoding is the process of transforming a street address
or other description of a location into a (latitude, longitude)
coordinate.
Reverse geocoding is the process of transforming a (latitude,
longitude) coordinate into a (partial) address. The amount of
detail in a reverse geocoded location description may vary,
for example one might contain the full street address of the
closest building, while another might contain only a city name
and postal code.
The Geocoder class requires a backend service that is not
included in the core android framework.
The Geocoder query methods will return an empty list if there
no backend service in the platform. Use the isPresent()
method to determine whether a Geocoder implementation
exists.
Syntax
Geocoder (Context context)
Syntax
Absolute Layout
● An Absolute Layout lets you specify exact locations (x/y
coordinates) of its children. Absolute layouts are less
flexible and harder to maintain than other types of layouts
without absolute positioning. AbsoluteLayout is based on
the simple idea of placing each control at an absolute
position.
● We specify for the exact x and y corodinates on the screen
for every control. So this recommended for most UI
development (in fact Absolute Layout is currentaly
deprecated)since absolute positioning of every element on
the screen makes an inflexible UI that is much more
difficult to maintain.
● Consider what happens if a control needs to be added to
the user interface UI, we would have to change the position
of every single element that is shifted by the new control.
This allows child views to be positioned at specified X and Y
coordinates within the containing layout view.
● <AbsoluteLayoutxmlns:android="http://
schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
● android:layout_height="fill_parent">
● <TextView
android:layout_x="110px"
android:layout_y="110px"
android:text="User Name"
android:layout_width="wrap_c
ontent"
● android:layout_height="wrap_content" />
● <EditText
android:layout_x="250p
x"
android:layout_y="80px
" android:width="100px"
android:layout_width="200
dp"
● android:layout_height="wrap_content" />
● <TextView
android:layout_x="110px"
android:layout_y="200px"
android:text="Password"
android:layout_width="wrap_c
ontent"
● android:layout_height="wrap_content" />
● <EditText
android:layout_x="250p
x"
android:layout_y="150p
x" android:width="100px"
android:layout_width="200
dp"
● android:layout_height="wrap_content" />
● <Button
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent" android:text="Log In"
android:layout_x="300px"
android:layout_y="300px"/>
● </AbsoluteLayout>
<TextView
android:id="@+id/tvDa
te"
android:layout_width=
"149dp"
android:layout_height
="46dp"
android:layout_marginEn
d="224dp"
android:layout_marginBotto
m="312dp"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/b
tnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="96dp"
android:layout_marginBottom="312dp"
android:text="Set Date"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="DuplicateClickableBoundsCheck" />
<DatePicker
android:id="@+id/dtpcke
r"
android:layout_width="3
14dp"
android:layout_height="
293dp"
android:layout_marginBotto
m="368dp"
android:datePickerMode="s
pinner"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.36"
app:layout_constraintStart_toStartOf="parent" />
<TimePicker
android:id="@+id/timepc
ker"
android:layout_width="1
84dp"
android:layout_height="
195dp"
android:layout_marginEnd=
"132dp"
android:layout_marginBotto
m="108dp"
android:timePickerMode="s
pinner"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/tvTime"
android:layout_width="130dp"
android:layout_height="
56dp"
android:layout_marginEnd
="232dp"
android:layout_marginBott
om="40dp"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/b
tnTime"
android:layout_width="wrap_co
ntent"
android:layout_height="wrap_co
ntent"
android:layout_marginEnd="104dp"
android:layout_marginBottom="48d
p" android:text="Set Time"
app:layout_constraintBottom_toBott
omOf="parent"
app:layout_constraintEnd_toEndOf=
"parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.datepickereg;
import
androidx.appcompat.app.AppCompatA
ctivity; import
android.app.DatePickerDialog;
import
android.app.TimePickerDial
og; import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button
;
import
android.widget.DatePicke
r; import
android.widget.TextView;
import
android.widget.TimePicke
r; import
java.util.Calendar;
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://
schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.
com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent
" tools:context=".MainActivity">
<TextView
android:id="@+id/textVie
w"
android:layout_width="81
dp"
android:layout_height="4
1dp"
android:layout_marginEn
d="268dp"
android:layout_marginBottom="576
dp" android:text="To :"
app:layout_constraintBottom_toBott
omOf="parent"
app:layout_constraintEnd_toEndOf=
"parent"/>
<TextView
android:id="@+id/textVie
w2"
android:layout_width="70
dp"
android:layout_height="4
3dp"
android:layout_marginEn
d="276dp"
android:layout_marginBottom="512
dp" android:text="Sms Text"
app:layout_constraintBottom_toBott
omOf="parent"
app:layout_constraintEnd_toEndOf=
"parent" />
<EditText
android:id="@+id/
etPhno"
android:layout_width="wrap_conten
t"
android:layout_height="wrap_conte
nt"
android:layout_marginEnd="40dp"
android:layout_marginBottom="572
dp" android:ems="10"
android:inputType="textPersonNam
e"
app:layout_constraintBottom_toBott
omOf="parent"
app:layout_constraintEnd_toEndOf=
"parent" />
<EditText
android:id="@+id/etm
sg"
android:layout_width=
"193dp"
android:layout_height
="51dp"
android:layout_marginEnd="56dp"
android:layout_marginBottom="504dp"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="SpeakableTextPresentCheck" />
<Button
android:id="@+id/btnSms"
android:layout_width="wrap_conten
t"
android:layout_height="wrap_conte
nt"
android:layout_marginEnd="156dp"
android:layout_marginBottom="400
dp" android:text="SEND SMS"
app:layout_constraintBottom_toBott
omOf="parent"
app:layout_constraintEnd_toEndOf=
"parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.testreceivesms;
import
androidx.appcompat.app.AppCompatA
ctivity; import
androidx.core.app.ActivityCompat;
import
androidx.core.content.ContextCo
mpat; import android.Manifest;
import android.content.IntentFilter;
import
android.content.pm.PackageMan
ager; import android.os.Bundle;
import
android.telephony.SmsMana
ger; import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText
; import
android.widget.Toast;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState
);
setContentView(R.layout.activity_m
ain);
fusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(this);
fetchLastLocation();
}
if
(ActivityCompat.checkSelfPermission(t
his,
Manifest.permission.ACCESS_FINE_LOC
ATION) !=
PackageManager.PERMISSION_GRANTE
D &&
ActivityCompat.checkSelfPermission(th
is,
Manifest.permission.ACCESS_COARSE_L
OCATION) !=
PackageManager.PERMISSION_GRANTE
D) {
ActivityCompat.requestPermissions(this,new
String[]
{Manifest.permission.ACCESS_FINE_LOCATION},REQUE
ST_CODE);
return;
}
Task<Location> task =
fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new
OnSuccessListener<Location>() { @Override
public void onSuccess(Location location) {
if(location!=null)
{
currentlocation=location;
Toast.makeText(getApplicationContext(),currentlocation
.getLatitude()+""+current location.getLongitude(),
Toast.LENGTH_SHORT).show();
SupportMapFragment supportMapFragment =
(SupportMapFragment)getSupportFragmentManager().
findFragmentById(R.id.go ogle_map);
supportMapFragment.getMapAsync(MainActivit
y.this);
}
}
});
}
@Override
public void onMapReady(@NonNull
GoogleMap googleMap) { LatLng
latLng=new
LatLng(currentlocation.getLatitude(),currentlocation.
getLongitude()); MarkerOptions
markerOptions=new
MarkerOptions().position(latLng)
.title("I am Here");
googleMap.animateCamera(CameraUpdateFactory.newLa
tLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLa
tLngZoom(latLng,5));
googleMap.addMarker(markerOptions);
}
@Override
public void onRequestPermissionsResult(int
requestCode, @NonNull String[] permissions, @NonNull
int[] grantResults) {
super.onRequestPermissionsResult(requestCode,
permissions, grantResults); switch (requestCode) {
case REQUEST_CODE:
if (grantResults.length > 0 &&
grantResults[0] ==
PackageManager.PERMISSION_GRANTED)
{ fetchLastLocation();
}
break;
}
}
}