Chapter 6 - WordPress.com

Download Report

Transcript Chapter 6 - WordPress.com

6: Getting Started with Android
Android
• Android Inc. was initially started in 2003
• It was acquired by Google in 2005
• The hardware side of Android is supported by
the Open Handset Alliance (OHA).
• Android had its fi rst major release in late
2008.
• The Android OS was built on a modified Linux
kernel and applications are written in Java.
WHO SUPPORTS ANDROID?
• HTC, LG, Motorola, and Samsung are the major
players in the Android smartphone market.
• Archos, Dell, Samsung, and Toshiba hold the
largest pieces of the Android tablet market.
• You should note that Amazon’s Kindle Fire and
Nook Color are up-and-comers and use a
customized version of the Android tablet (Version
3) OS on their devices
ANDROID AS COMPETITION TO ITSELF
• Because Android was designed to be run on many
different types of devices.
• Because of the open nature of the Android OS, it is
commonplace for manufacturers to create vendorspecific builds of Android, and when this happens you
are beholden to them for OS updates.
• Additionally in these custom builds, vendor-specific
limitations have arisen such as the vendor-specific
market.
• Another issue that has cropped up is the lack of overthe-air (OTA) distribution of OS updates by cellular
carriers.
Multiple Markets and Market Locks
• Depending on your version of Android, and depending on the
manufacturer of a given device.
• You often find on cheap and imported Android devices a version of
Google Play that is maintained by the manufacturer.
• They pick and choose what applications are available from the
whole set in the marketplace.
• The version of the Android SDK that you need to support depends
on what devices you want to
support.
• If you want to target most phones available right now, you should
support Android 2.2 or 2.3. “Gingerbread” (2.3) is the last widely
available version for those devices.
• Once you have decided on a version to deploy your application
against, you need to set up your development environment.
GETTING THE TOOLS YOU NEED
•
•
•
•
Downloading and Installing JDK
Downloading and Installing Eclipse
Downloading and Installing the Android SDK
Downloading and Configuring the Eclipse ADT
Plug-in
• Installing Additional SDK Components
• Development
• BUILDING THE DERBY APP IN ANDROID
Downloading and Installing JDK
Downloading and Installing Eclipse
Downloading and Installing the
Android SDK
Downloading and Configuring the
Eclipse ADT Plug-in
Downloading and Configuring the
Eclipse ADT Plug-in
• Use the Update Manager feature of your Eclipse installation to install the
latest revision of ADT on
• your development computer. Follow these steps:
• 1. Start Eclipse and select Help ➪ Install New Software.
• 2. Click Add in the top-right corner.
• 3. In the Add Repository dialog box that appears, enter ADT plug-in for the
name and the following URL for the location: https://dlssl.google.com/android/eclipse/.
• 4. Click OK. If you have trouble acquiring the plug-in, try using “http” in the
Location URL instead of “https” (“https” is preferred for security reasons).
• 5. In the Available Software dialog box, select the checkbox next to
Developer Tools and click Next.
• 6. The next window shows a list of the tools to be downloaded. Click Next.
• 7. Read and accept the license agreements and then click Finish. If you get a
security warning saying that the authenticity or validity of the software
can’t be established, click OK.
• 8. When the installation completes, restart Eclipse.
Downloading and Configuring the
Eclipse ADT Plug-in
• After you’ve successfully downloaded the ADT, the next step
is to modify your ADT preferences in Eclipse to point to the
Android SDK directory (see Figure 6-5):
• 1. Select Window ➪ Preferences to open the Preferences
panel. In Mac OS X, click Eclipse ➪ Preferences.
• 2. Select Android from the left panel.
• 3. You may see a dialog box asking whether you want to
send usage statistics to Google. If so, make your choice and
click Proceed. You cannot continue with this procedure until
you click Proceed.
• 4. For the SDK Location in the main panel, click Browse and
locate your downloaded SDK directory.
• 5. Click Apply and then click OK.
Downloading and Configuring the
Eclipse ADT Plug-in
Installing Additional SDK Components
Installing Additional SDK Components
• Loading the Android SDK Manager in Eclipse takes only a few steps:
1. Open Eclipse.
2. Select Window ➪ Android SDK and AVD Manager.
3. Select Available Packages in the left panel. This reveals all of the
components that are currently available for download from the SDK
repository.
4. Select the component(s) you’d like to install and click Install
Selected.
5. Verify and accept the components you want (ensure each one is
selected with a green checkmark) and click Install. The components
will now be installed into your existing Android
SDK directories.
Development - Creating a New Project
Development – Naming your project
Development – configuring application
information
Development – choosing sdk version for
your app
Project Structure
• All of your code lives within your
src folder.
• The res folder holds layouts and
resources for different hardware
specs.
• The layout subfolder holds all of
your XML
layouts.
• All of your XML layouts are stored
in the layout subfolder of res, and
your code will be linked under the
namespace in your src folder of the
project view.
Project Structure
• All of your code lives within your
src folder.
• The res folder holds layouts and
resources for different hardware
specs.
• The layout subfolder holds all of
your XML
layouts.
• All of your XML layouts are stored
in the layout subfolder of res, and
your code will be linked under the
namespace in your src folder of the
project view.
Project Structure – Manifest File
Project Structure – Manifest File –
Application Editor
Project Structure – Manifest File –
Application Editor
Project Structure – Manifest File –
Permissions Editor
Project Structure – Manifest File xml
Android Basics - Creating User Interfaces Basic Android UI Elements
Android Basics - Creating User Interfaces Basic Android UI Elements
Android Basics - Creating User Interfaces Basic Android UI Elements
Android Basics - Creating User Interfaces Basic Android UI Elements
Android Basics - Creating User Interfaces Basic Android UI Elements
Android Basics - Creating User Interfaces Basic Android UI Elements
Android Basics - Creating User Interfaces Basic Android Layouts and Views
• ➤ FrameLayout: This is very simplistic and can really contain
only a single UI element. You can, in fact, have multiple
elements but they overlap each other by default.
• The example code shown here is rendered in Figure 6-17:
Android Basics - Creating User Interfaces Basic Android Layouts and Views
Android Basics - Creating User Interfaces Basic Android Layouts and Views
Android Basics - Creating User Interfaces Basic Android Layouts and Views
Android Basics - Creating User Interfaces Basic Android Layouts and Views
Creating an Android Virtual Device
• You need to create an Android Virtual Device (AVD) in
order to debug your application in the emulator,
because this “device” is what the emulator runs.
• Creating an AVD is quite easy.
• Eclipse includes a tool called AVD Manager (click
Window Manager ➪ AVD Manager).
• You need to name your AVD instance, choose its OS
version (Target), pick a skin (with which you can
customize the look and feel of the emulator) and
resolution, and specify the hardware details for the
device (amount of RAM, size of SD card, and sensors
like Accelerometer and GPS).
• Once you have configured it to your specifications, click
Create AVD and you are all set.
Debugging
Debugging
CONNECTING TO THE GOOGLE PLAY
• Getting Signing Your Application
• Signing your application with Eclipse is a relatively
simple process:
1. Right-click your project in the Package Explorer
and select File ➪ Export.
2. Select Export Android Application.
3. Complete the steps of the wizard and you will
have a keystore, and a signed release build of
your app ready for the market.ng an Android
Developer Account
CONNECTING TO THE GOOGLE PLAY
• ANDROID DEVELOPMENT PRACTICES
• Android Fundamentals
– Activities
• Activities are the individual screens in your application.
– Services
• Services are components that run in a background thread.
– Content Providers
• Content providers are interfaces to the offl ine storage that
you have within your app.
– Broadcast Receivers
• Broadcast receivers are components that respond to system
messages.
CONNECTING TO THE GOOGLE PLAY
• Fragments as UI Elements
– Starting in Android 3.x, there has been a shift in design elements to account
for the significant differences between the screen sizes of tablets versus
handsets.Whereas normally UI design for mobile devices is very rigid,
Fragments add a level of fl exibility. Fragments themselves live as a subactivity
that you can reference in multiple places in your application. Fragments live
within the scope of their parent activity, but can be used in multiple activities.
• Ask for Permission
– The users of your application must approve of what functionality you want to
leverage on their device. To prompt the user for what you need, and so that
your device will behave as designed, you need to add permission requests in
your application’s manifest. Visit http://developer.android
.com/reference/android/Manifest.permission.html for a list the various
permissions you can request when developing.
– Depending on what version of the OS you are targeting you are afforded
additional permissions.
– One of the newest permissions available is READ_SOCIAL_STREAM, which
enables you to access the user’s social stream. One of the oldest permissions
is your ability to set the given time zone, using, you guessed, it
SET_TIME_ZONE.
• Mind the Back Stack
– Unlike iOS-based devices, all Android devices have a hardware back button.
BUILDING THE DERBY APP IN
ANDROID - Common Interactions
• UI Navigation and Using Back Stack
– Use the GetExtra command to retrieve simple and complex objects
from one activity to another using an Intent.
• You can select the extras you want to refer to individually from the
Intent. For example:
String id = getIntent().getStringExtra(“id”);
String name = getIntent().getStringExtra(“name”)
• Or you can get all of the objects you passed along as a Bundle:
Bundle extras = getIntent().getExtras();
String userName;
String id;
if (extras != null) {
userName = extras.getString(“name”);
id = extras.getString(“id”);
}
BUILDING THE DERBY APP IN
ANDROID - Common Interactions
• Use the PutExtra command to put the object you
want to pass between activities. You can pass
simple or complex objects. You need to use Extras
only when you want to pass data between
activities. It is not necessary to set a complex
state transfer process if you don’t need that data.
BUILDING THE DERBY APP IN
ANDROID - Notifications
• Toast
• Alerts
BUILDING THE DERBY APP IN
ANDROID - Notifications
• Alerts
BUILDING THE DERBY APP IN
ANDROID - Ofline Storage
• SQLite
BUILDING THE DERBY APP IN
ANDROID - Offline Storage
• SQLite
BUILDING THE DERBY APP IN
ANDROID - Offline Storage
• SQLite
BUILDING THE DERBY APP IN
ANDROID - SharedPreferences
• SharedPreferences is a set of key-value pairs saved on your
device that is helpful for storing instance-specific data as it
pertains to the app. The main concern is the level of privacy
that you impose upon it. If you make it world-readable its value
can be accessed by any application should another application
query against your key.
BUILDING THE DERBY APP IN
ANDROID - Web Service
BUILDING THE DERBY APP IN
ANDROID - Web Service
BUILDING THE DERBY APP IN
ANDROID - Web Service
• Long Running Task on Web
BUILDING THE DERBY APP IN
ANDROID - GPS
• public class SensorsGPS extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new
MyLocationListener();
locManager.requestLocationUpdates(LocationManager.
GPS_PROVIDER, 0, 0, locListener);
}
BUILDING THE DERBY APP IN
ANDROID - Accelerometer
public class SensorsAccel extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* do this in onCreate */
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE
mSensorManager.registerListener(mSensorListener, mSensorManager.
getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
mAccel = 0.00f;
mAccelCurrent = SensorManager.GRAVITY_EARTH;
mAccelLast = SensorManager.GRAVITY_EARTH;
}
BUILDING THE DERBY APP IN
ANDROID - Accelerometer
BUILDING THE DERBY APP IN
ANDROID - Accelerometer
Thank You