5-6 Chapter QSTN Answer
5-6 Chapter QSTN Answer
Returns the approximate distance in meters between this location and the given location.
2 float getAccuracy()
3 double getAltitude()
4 float getBearing()
5 double getLatitude()
6 double getLongitude()
7 float getSpeed()
Background
A background service performs an operation that isn't directly noticed by the user. For
example, if an app used a service to compact its storage, that would usually be a background
service.
Bound
A service is bound when an application component binds to it by calling bindService(). A
bound service offers a client-server interface that allows components to interact with the
service, send requests, receive results, and even do so across processes with interprocess
communication (IPC).
2) Position Sensors: These are used to measure the physical position of device.
3) Environmental Sensors: These are used to measure the environmental changes such as
temperature, humidity etc.
Android provides facility to integrate Google map in our application. Google map displays
your current location, navigate location direction, search location etc. We can also
customize Google map according to our requirement.
There are four different types of Google maps, as well as an optional to no map at all. Each
of them gives different view on map. These maps are as follow:
1. Normal: This type of map displays typical road map, natural features like river and some
features build by humans.
2. Hybrid: This type of map displays satellite photograph data with typical road maps. It also
displays road and feature labels.
3. Satellite: Satellite type displays satellite photograph data, but doesn't display road and
feature labels.
4. Terrain: This type displays photographic data. This includes colors, contour lines and
labels and perspective shading.
5. None: This type displays an empty grid with no tiles loaded.
Geocoding is the process of converting addresses (like a street address) into geographic
coordinates (like latitude and longitude), which you can use to place markers on a map, or
position the map.
1. Telephony.Sms.Conversations
Contains a view of SMS conversations (also referred to as threads).
2. Telephony.Sms.Draft
Contains all draft text-based SMS messages in the SMS app.
3. Telephony.Sms.Inbox
Contains all text-based SMS messages in the SMS app inbox.
4. Telephony.Sms.Intents
Contains constants for SMS related Intents that are broadcast.
5. Telephony.Sms.Outbox
Contains all pending outgoing text-based SMS messages.
6. Telephony.Sms.Sent
Contains all sent text-based SMS messages in the SMS app.
4 Marks
A Content Provider component supplies data from one application to others on request. Such
requests are handled by the methods of the ContentResolver class. A content provider can use
different ways to store its data and the data can be stored in files, in a database or even over
a network. Content Providers support the four basic operations, normally called CRUD-
operations. CRUD is the acronym for create, read, update and delete.
onResume called when activity will start interacting with the user.
4. Discuss the need of permissions in Android. Describe the permissions to set system
functionalities like Bluetooth, camera.
The purpose of a permission is to protect the privacy of an Android user. Android apps must
request permission to access sensitive user data (such as contacts and SMS), as well as certain
system features (such as camera and internet).
Location Permission - If your application tags images with GPS location information, you must
request the ACCESS_FINE_LOCATION permission.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...
<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
It is embedded in android by default. So, there is no need to perform any database setup or
administration task. SQLiteDatabase has methods to create, delete, execute SQL commands,
and perform other common database management tasks.
The Android Things Console provides easy and secure deployment of updates to your
connected devices. Google provides the infrastructure to host and deliver system and app
updates with the developer in final control.
• Motion sensors
• Photometer is used to sense and control the brightness. Besides, there are sensors for
pressure, humidity, and temperature.
• For movements, Accelerometer is used to detect shakes/tilt gestures.
• Proximity Sensors are used to detect how close the object is to the device. It’s commonly
present in Call Applications. As you bring the phone close to the ear, the screen goes black,
thanks to this sensor. Though the maximum range of proximity is 5 cms.
• Gyroscope is used to measure rotation/spin. Gravity sensors are used to measure the force
of gravity.
• Magneto Meter is used to get the device position.
• Pedometer is used to detect the number of steps the user takes.
1. hide(): This method is used to hide the ZoomControls from the screen. In some cases we
need to hide the ZoomControls from the screen so that we use this function.
2. show(): This method is used to show the ZoomControls which we hide from the screen by
using hide method.
3. setOnZoomInClickListener(OnClickListenerlistener): This is a listener event automatically
called when we click on the Zoom In button of ZoomControls. In this listener we add the code
to zoom in image.
4. setOnZoomOutClickListener(OnClickListenerlistener): This is a listener event
automatically called when we click on the Zoom Out button of ZoomControls. In this listener
we add the code for zoom out a image.
5. setIsZoomInEnabled(boolean isEnabled): This method is used to enable or disable the
zoom In button of ZoomControls. In this method we set a Boolean value either true or false.
By default it has true value but sometime after a limit of zoom in we need to disable the zoom
in functionality i.e. after that we didn’t need more zoom in.
There can be two forms of a service. The lifecycle of service can follow two different paths: started or bound.
1) Started Service
A service is started when component (like activity) calls startService() method, now it runs in the background
indefinitely. It is stopped by stopService() method. The service can stop itself by calling the stopSelf() method.
2) Bound Service
A service is bound when another component (e.g. client) calls bindService() method. The client can unbind the
service by calling the unbindService() method. The service cannot be stopped until all clients unbind the
service.
Like any other components service also has callback methods. These will be invoked while
the service is running to inform the application of its state.
onCreate()
This is the first callback which will be invoked when any component starts the service. If the
same service is called again while it is still running this method wont be invoked. Ideally one
time setup and intializing should be done in this callback.
onStartCommand()
This callback is invoked when service is started by any component by calling startService(). It
basically indicates that the service has started and can now run indefinetly.
onBind()
This is invoked when any component starts the service by calling onBind.
onUnbind()
This is invoked when all the clients are disconnected from the service.
onRebind()
This is invoked when new clients are connected to the service. It is called after onUnbind
onDestroy()
This is a final clean up call from the system. This is invoked just before the service is being
destroyed. Could be very useful to cleanup any resources such as threads, registered
listeners, or receivers.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fill_parent"
android:layout_height="fill_parent"
tools:context="example.javatpoint.com.fragmentexample.MainActivity">
<fragment
android:id="@+id/fragment1"
android:name="example.javatpoint.com.fragmentexample.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment
android:id="@+id/fragment2"
android:name="example.javatpoint.com.fragmentexample.Fragment2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
The following classes are used to play sound and video in the Android framework:
MediaPlayer
This class is the primary API for playing sound and video. An object of this class can fetch,
decode, and play both audio and video with minimal setup. It supports several different
media sources such as:
AudioManager
For example,
SQLiteDatabase db;
db=openOrCreateDatabase(String path, int mode, SQLiteDatabase.CursorFactory factory) ;
This method is used to create/open database. As the name suggests, it will open a database
connection if it is already there, otherwise, it will create a new one.
Arguments:
String path: Name of the database
Int mode: operating mode. Use 0 or "MODE_PRIVATE" for the default operation, or
"CREATE_IF_NECESSARY" if you like to give an option that "if a database is not there, create
it"
CursorFactory factory: An optional factory class that is called to instantiate a cursor when a
query is called
Example,
db=openOrCreateDatabase("XYZ_Database",SQLiteDatabase.CREATE_IF_NECESSARY,null);
For Example,
BEGIN TRANSACTION;
UPDATE accounts
SET balance = balance - 1000
WHERE account_no = 100;
UPDATE accounts
SET balance = balance + 1000
WHERE account_no = 200;
COMMIT;
LocationManager – This class helps to get access to the location service of the system.
LocationListener – This interface acts as the listener which receives notification from the
location manager when the location changes or the location provider is disabled or enabled.
Location – This is the class which represents the geographic location returned at a particular
time.
Methods:
16. Develop an application to send SMS. (Write ONLY .java and permission tag in manifest file)
***to answer this question write only the code which is in bold style***
Send SMS
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javapapers.androidsms">
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".SendSmsActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_sms.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/toPhoneNumberTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number" />
<EditText
android:id="@+id/toPhoneNumberET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"/>
<TextView
android:id="@+id/smsMessageTV"
android:layout_width="wrap_content"
<EditText
android:id="@+id/smsMessageET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>
<Button android:id="@+id/sendSMSBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send SMS"/>
</LinearLayout>
SendSmsActivity.java
package com.javapapers.androidsms;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
Button sendSMSBtn;
EditText toPhoneNumberET;
EditText smsMessageET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
sendSMSBtn = (Button) findViewById(R.id.sendSMSBtn);
toPhoneNumberET = (EditText) findViewById(R.id.toPhoneNumberET);
smsMessageET = (EditText) findViewById(R.id.smsMessageET);
sendSMSBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMS();
}
});
}
Receive SMS
AndroidManifest.xml
</application>
</manifest>
SmsBroadcastReceiver.java
package com.javapapers.androidreceivesms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
SmsActivity.java
package com.javapapers.androidreceivesms;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;
@Override
public void onStart() {
super.onStart();
inst = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
smsListView = (ListView) findViewById(R.id.SMSList);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
smsMessagesList);
smsListView.setAdapter(arrayAdapter);
smsListView.setOnItemClickListener(this);
refreshSmsInbox();
}
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
try {
String[] smsMessages = smsMessagesList.get(pos).split("\n");
String address = smsMessages[0];
String smsMessage = "";
for (int i = 1; i < smsMessages.length; ++i) {
smsMessage += smsMessages[i];
}
activity_sms.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SMS Inbox"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
<ListView android:id="@+id/SMSList"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="5dp" />
</LinearLayout>
6 Marks
Button button ;
ImageView imageView ;
Intent intent ;
public static final int RequestPermissionCode = 1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView);
EnableRuntimePermission();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(intent, 7);
}
});
}
imageView.setImageBitmap(bitmap);
}
}
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.CAMERA))
{
} else {
ActivityCompat.requestPermissions(MainActivity.this,new String[]{
Manifest.permission.CAMERA}, RequestPermissionCode);
}
}
@Override
public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {
switch (RC) {
case RequestPermissionCode:
} else {
}
break;
}
}
activity_main.xml layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.android_examples.captureimagecamera_android_examplescom.MainA
ctivity"
android:orientation="vertical"
android:background="#FFF9C4">
<ImageView
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:id="@+id/imageView" />
<Button
android:text="Click here to capture image using camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
</LinearLayout>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
</manifest>
activity_main.xml
Drag the 2 EditTexts, 1 MultiLine EditText, 3 TextViews and 1 Button from the pallete, now
the activity_main.xml file will like this:
File: activity_main.xml
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="22dp"
android:layout_marginTop="16dp"
android:ems="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="18dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="To:" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:text="Subject:" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"
android:layout_alignParentLeft="true"
android:text="Message:" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginLeft="76dp"
android:layout_marginTop="20dp"
android:text="Send" />
</RelativeLayout>
Activity class
File: MainActivity.java
package com.example.sendemail;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
editTextTo=(EditText)findViewById(R.id.editText1);
editTextSubject=(EditText)findViewById(R.id.editText2);
editTextMessage=(EditText)findViewById(R.id.editText3);
send=(Button)findViewById(R.id.button1);
send.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
String to=editTextTo.getText().toString();
String subject=editTextSubject.getText().toString();
String message=editTextMessage.getText().toString();
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
3. Write a program to locate user’s current location. (Write ONLY .java and manifest file)
Androidmanifest.xml
Mainactivity.java
package com.javapapers.android.geolocationfinder;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
import android.util.Log;
@Override
@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}
}