This Study Resource Was: Chapter 1 Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CHAPTER 1 ANSWERS

1. What is an AVD?
An AVD is an Android Virtual Device. It represents an Android emulator,
which
Emulates a particular configuration of an actual Android device.
2. What is the difference between the android version Code and
android version Name attributes
In the AndroidManifest.xml file?
The android version Code attribute is used to programmatically check
whether an
Application can be upgraded. It should contain a running number (an
updated application
Is set to a higher number than the older version). The android version Name
attribute is
Used mainly for displaying to the user. It is a string, such as “1.0.1”.
3. What is the use of the strings.xml file?

m
er as
The strings.xml fi le is used to store all string constants in your application.

co
eH w
This enables you to easily localize your application by simply replacing the
strings and then Recompiling your application.

o.
CHAPTER 2 ANSWERS rs e
ou urc
1. What will happen if you have two or more activities with the same
intent filter action name?
The Android OS will display a dialog from which users can choose which
o

activity they
aC s

Want to use.
vi y re

2. Write the code to invoke the built-in Browser application?


Use the following code:
Intent i = new
Intent (android .content .Intent. ACTION_VIEW,
ed d

Uri .parse (“http://www.amazon.com”));


ar stu

Start Activity (i);


3. Which components can you specify in an intent filter?
In an intent filter, you can specify the following: action, data, type, and
is

category.
4. What is the difference between the Toast class and the
Th

Notification Manager class?


The Toast class is used to display alerts to the user; it disappears after a few
seconds. The
sh

Notification Manager Class is used to display notifications on the device’s


status bar.
The alert displayed by the Notification Manager class is persistent and can
only be
Dismissed by the user when selected.
5. Name the two ways to add fragments to an activity?

This study source was downloaded by 100000827613812 from CourseHero.com on 06-07-2021 22:09:11 GMT -05:00

https://www.coursehero.com/file/59069423/short-questiondocx/
You can either use the <fragment> element in the XML fi le, or use the
Fragment Manager
And Fragment Transaction classes to dynamically add/remove fragments
from an activity.
6. Name one key difference between a fragment and an activity?
One of the main differences between activities and fragments is that when
an activity goes
Into the background, the activity is placed in the back stack. This allows an
activity to be
Resumed when the user presses the Back button. Conversely, fragments are
not automatically
Placed in the back stack when they go into the background.

CHAPTER 3 ANSWERS

m
er as
1. What is the difference between the dp unit and the PX

co
Unit? Which one should you use to specify the dimension of a view?

eH w
The dp unit is density independent and 1dp is equivalent to one pixel on a
160 dpi screen. The

o.
px unit corresponds to an actual pixel on screen. You should always use the
dp unit because it rs e
ou urc
Enables your activity to scale properly when run on devices of varying screen
size.
2. Why is the Absolute Layout not recommended for use?
o

With the advent of devices with different screen sizes, using the Absolute
aC s

Layout makes it
vi y re

difficult for your application to have a consistent look and feel across
devices.
3. What is the difference between the on Pause () method and the
ed d

on Save Instance State ()


Method?
ar stu

The on Pause () event is fi red whenever an activity is killed or sent to the


background. The
is

On Save Instance State () event is like the on Pause () event, except that it is
not always
Th

Called, such as when the user presses the back button to kill the activity.
4. Name the three methods you can override to save an activity’s
state. In what instances should you
sh

Use the various methods?


The three events are on Pause (), on Save Instance State (), and
On Retain Non Configuration Instance (). You generally use the on Pause ()
method
To preserve the activity’s state because the method is always called when
the activity is

This study source was downloaded by 100000827613812 from CourseHero.com on 06-07-2021 22:09:11 GMT -05:00

https://www.coursehero.com/file/59069423/short-questiondocx/
About to be destroyed. However, for screen orientation changes, it is easier
to use the
On Save Instance State () method to save the state of the activity (such as
the data entered
By the user) using a Bundle object. The on Retain Non Configuration Instance
() method is
Useful for momentarily saving data (such as images or fi les downloaded
from a web service)
Which might be too large to fi t into a Bundle object.
5. How do you add action items to the Action Bar?
Adding action items to the Action Bar is similar to creating menu items for
an options
Menu — simply handle the on Create Options Menu () and on Options Item
Selected () events.

m
CHAPTER 4 ANSWERS

er as
1. How do you programmatically determine whether a Radio Button

co
is checked?

eH w
You should check the is Checked () method of each Radio Button to

o.
determine whether it
has been checked. rs e
ou urc
2. How do you access the string resource stored in the strings.xml
file?
You can use the get Resources () method.
o

3. Write the code snippet to obtain the current date?


aC s

The code snippet to obtain the current date is as follows:


vi y re

//—-get the current date—-


Calendar today = Calendar .get Instance ();
YR = today. Get (Calendar. YEAR);
Month = today .get (Calendar .MONTH);
ed d

Day = today .get (Calendar. DAY_OF_MONTH);


ar stu

Show Dialog (DATE_DIALOG_ID);


4. Name the three specialized fragments you can use in your
Android application and describe
is

Their uses?
Th

The three specialized fragments are List Fragment, Dialog Fragment, and
Preference Fragment. The List Fragment is useful for displaying a list of
items, such as
sh

An RSS listing of news items. The Dialog Fragment allows you to display a
dialog window
Modally and is useful to get a response from the user before allowing him to
continue

This study source was downloaded by 100000827613812 from CourseHero.com on 06-07-2021 22:09:11 GMT -05:00

https://www.coursehero.com/file/59069423/short-questiondocx/
With your application. The Preference Fragment displays a window containing
your
Application’s preferences and allows the user to edit them directly in your
application.

CHAPTER 5 ANSWERS
1. What is the purpose of the Image Switcher?
The Image Switcher enables images to be displayed with animation. You can
animate the
Image when it is being displayed, as well as when it is being replaced by
another image.
2. Name the two methods you need to override when implementing
an options menu in your activity?
The two methods are on Create Options Menu () and on Options Item
Selected ().
3. Name the two methods you need to override when implementing

m
er as
a context menu in your activity?
The two methods are on Create Context Menu () and on Context Item

co
eH w
Selected ().
4. How do you prevent the Web View from invoking the device’s web

o.
browser when a redirection
Occurs in the Web View? rs e
ou urc
To prevent launching the device’s web browser, you need to implement the
Web View Client
o

Class and override the should Override Ur Loading () method.


aC s
vi y re

CHAPTER 6 ANSWERS
1. How do you display the preferences of your application using an
activity?
You can do so using the Preference Activity class.
ed d

2. Name the method that enables you to obtain the path of the
ar stu

external storage of an Android


Device?
The method name is get External Storage Directory ().
is

3. What is the permission you need to declare when writing fi les to


external storage?
Th

The permission is WRITE_EXTERNAL_STORAGE.


CHAPTER 7 ANSWERS
1. Write the query to retrieve all contacts from the Contacts
sh

application that contain the word “jack.”?


The code is as follows:
Cursor c;
if (android.os.Build.VERSION.SDK_INT <11) {
//---before Honeycomb---
c = managedQuery(allContacts, projection,

This study source was downloaded by 100000827613812 from CourseHero.com on 06-07-2021 22:09:11 GMT -05:00

https://www.coursehero.com/file/59069423/short-questiondocx/
ContactsContract.Contacts.DISPLAY_NAME + “ LIKE ?”,
new String[] {“%jack”},
ContactsContract.Contacts.DISPLAY_NAME + “ ASC”);
} else {
//---Honeycomb and later---
CursorLoader cursorLoader = new CursorLoader(
this,
allContacts,
projection,
ContactsContract.Contacts.DISPLAY_NAME + “ LIKE ?”,
new String[] {“%jack”},
ContactsContract.Contacts.DISPLAY_NAME + “ ASC”);
c = cursorLoader.loadInBackground();
}
2. Name the methods that you need to override in your own
implementation of a content provider?

m
er as
The methods are getType(), onCreate(), query(), insert(), delete(), and
update().

co
eH w
3. How do you register a content provider in your
AndroidManifest.xml file?

o.
The code is as follows:
rs e
<provider android:name=”BooksProvider”
ou urc
android:authorities=”net.learn2develop.provider.Books” />
CHAPTER 8 ANSWERS
o

1. Name the two ways in which you can send SMS messages in your
aC s

Android application?
vi y re

You can either programmatically send an SMS message from within your
Android application
or invoke the built-in Messaging application to send it on your
ed d

application’s behalf.
2. Name the permissions you need to declare in your
ar stu

AndroidManifest.xml fi le for sending and


receiving SMS messages?
The two permissions are SEND_SMS and RECEIVE_SMS.
is

3. How do you notify an activity from a BroadcastReceiver?


The Broadcast receiver should fi re a new intent to be received by the
Th

activity. The activity


should implement another BroadcastReceiver to listen for this new intent.
CHAPTER 9 ANSWERS
sh

1. If you have embedded the Google Maps API into your Android
application but it does not show
the map when the application is loaded, what could be the likely
reasons?
The likely reasons are as follows:
➤ No Internet connection

This study source was downloaded by 100000827613812 from CourseHero.com on 06-07-2021 22:09:11 GMT -05:00

https://www.coursehero.com/file/59069423/short-questiondocx/
➤ Incorrect placement of the <uses-library> element in the
AndroidManifest.xml fi le
➤ Missing INTERNET permission in the AndroidManifest.xml fi le
2. What is the difference between geocoding and reverse
geocoding?
Geocoding is the act of converting an address into its coordinates
(latitude and longitude).
Reverse geocoding converts a pair of location coordinates into an
address.
3. Name the two location providers that you can use to obtain your
location data?
The two providers are as follows:
➤ LocationManager.GPS_PROVIDER
➤ LocationManager.NETWORK_PROVIDER

4. What method is used for monitoring a location?

m
er as
The method is addProximityAlert().
CHAPTER 10 ANSWERS

co
eH w
1. Name the permissions you need to declare in your
AndroidManifest.xml file for an HTTP

o.
connection?
rs e
ou urc
The permission is INTERNET.
2. Name the classes used for dealing with JSON messages?
The classes are JSONArray and JSONObject.
3. Name the class for performing background asynchronous tasks?
o

The class is AsyncTask.


aC s

CHAPTER 11 ANSWERS
vi y re

1. Why is it important to put long-running code in a service on a


separate thread?
A separate thread should be used because a service runs on the same
ed d

process as the calling


ar stu

activity. If a service is long-running, you need to run it on a separate


thread so that it does
not block the activity.
2. What is the purpose of the IntentService class?
is

The IntentService class is similar to the Service class, except that it runs
Th

the tasks in a
separate thread and automatically stops the service when the task has
finished execution.
3. Name the three methods you need to implement in an AsyncTask
sh

class?
The three methods are doInBackground(), onProgressUpdate(), and
onPostExecute().
4. How can a service notify an activity of an event happening?
The service can broadcast an intent, and the activity can register an
intent using an

This study source was downloaded by 100000827613812 from CourseHero.com on 06-07-2021 22:09:11 GMT -05:00

https://www.coursehero.com/file/59069423/short-questiondocx/
IntentFilter class.
5. For threading, what is the recommended method to ensure that
your code runs without tying up
the UI of your application?
The recommended method is to create a class that subclasses the
AsyncTask class. This will
ensure that the UI is updated in a thread-safe manner.
CHAPTER 12 ANSWERS
1. How do you specify the minimum version of Android required by
your application?
You specify the minimum Android version required using the
minSdkVersion attribute in
the AndroidManifest.xml file.
2. How do you generate a self-signed certificate for signing your
Android application?
To generate a certificate, you can either use the keytool.exe utility from

m
er as
the Java SDK or

co
use Eclipse’s Export feature.

eH w
3. How do you configure your Android device to accept applications
from non-Market sources?

o.
rs e
Go to the Settings application and select the Security item. Check the
ou urc
“Unknown sources” item.
o
aC s
vi y re
ed d
ar stu
is
Th
sh

This study source was downloaded by 100000827613812 from CourseHero.com on 06-07-2021 22:09:11 GMT -05:00

https://www.coursehero.com/file/59069423/short-questiondocx/
Powered by TCPDF (www.tcpdf.org)

You might also like