Download Android - WordPress.com

Document related concepts
no text concepts found
Transcript

Android is a Linux-based operating
system
designed
primarily
for
touchscreen mobile devices such as
smartphones and tablet computers.

It is currently developed by Google in
conjunction with the Open Handset
Alliance.[2]

Initially developed by Android Inc,
whom Google financially backed and
later purchased in 2005,
Open Source:

Google releases the Android code as
open source, under the Apache License.
 The Android Open Source Project
(AOSP), led by Google, is responsible
with
the
maintenance
and
further
development of Android.
Android Apps:

Additionally,
community
Android
of
has
developers
a
large
writing
applications ("apps") that extend the
functionality
of
devices,
written
primarily in a customized version of
Java.
Creating Android Application:
 Download the Android SDK.
 Install the ADT plugin for Eclipse (if
you’ll use the Eclipse IDE).
 Download the latest SDK tools and
platforms using the SDK Manager.
What is Android?
 Android is an operating system which is
based on Linux with Java Programming
Interface.
Android SDK:
 The Android Software Development kit
provides the following necessary tools
for
the
developing
Applications.
 A Compiler
 Debugger
 A device emulator
the
Android
 It’s own virtual Machine to run Android
Programs
 Android is primarily developed by
Google.
 It Allows:
 Background Processing
 Provides a rich user Interface
library
 Supports 2-D and 3-D Graphics
using the Open GL Libraries
 Access to the file system
 An embedded SQLite database
 Android Application can re-use
components of other applications
using its different components.
Android Components:
 Activity
It represents the presentation layer of an
Android application. It presents a screen in
your application. Activity can be displayed
as dialogues or can be Transparent. An
Android application can have several
activities.
 Views and View Groups:
“Views” are user interface widgets. The
base class for all the views is android
.View.views has attributes which is used
to
configure
their
appearance
and
behaviour.
A “View Groups” is for arranging other
views. They are also called as layout
managers the base class for “view Groups”
I android.View.viewgroup which extends
the
class.
In order create complex layout View
Groups can be nestled. It should not too
deeply nestled since has a negative impact
on the performance.
 Intents:
These are asynchronous message which
allows application in order to request
functionality from other components of the
Android
Systems.
Applications register themselves to intent
via an Intents filter. To perform certain
tasks Intents allow to combine loosely
coupled Components.
 Services:
Services
perform
background
tasks
without providing a user Interface can
notify the user via the notification
framework in Android.
 Content Provider:
It provider a structured interface to
application data via a content provider
applications can share data from one to
another. Along with content provider SQ
Lite database is used in conjunction. The
SQ Lite database stores the data which is
accessed by content provider.
 Broadcast Receiver
Broadcast receiver registers the messages
and Intents In the case when specified
situation happens a Broadcast receiver will
get notified by the Android system.
 Widgets (Home Screen)
These are interactive components used on
the Android home screen. They display a
kind of data and allow user to perform via
them.
Android Development Tools
Android SDK
 The Android software Development kit
(SDK) contains tools to:
 Create application
 Compile application
 Package application
 Most of these are command line based
the android SDK also provides.
 Android device emulator can be tested
without a real Android phone.
 You can create Android Virtual devices
via the Android SDK which runs
emulator.
ADB:
 The Android SDK Contains the Android
debug bridge (ADB) tool which along to
connect to a virtual or real Android
Device.
Android development Tools (ADT)
 Google
provides
ADT
to
develop
Android applications with Eclipse. It is a
set of components which extends the
Eclipse
with
Android
development
capabilities.
 ADT contains all required functionalities
to,
 Create
 Compile
 Debug
 A deploy Android applications
from the eclipse IDE, ADT
allows to create and start AVDs.
Dalvik Virtual Machine
 The
special
virtual
machine
used
Android system is Dalvik to run Java
based applications. It uses its own byte
code format which different from java
byte code.
 Java files cannot run on Android
directly. They Should be converted in
the Dalvik byte code format.
How to develop Android Application?
 Android
Applications
are
primarily
written in Java programming language.
The Java source files are converted to
java class files by the java compiler.
 The Android SDK Contains a tool called
“dx” which converts Java files to “.dex”
(Dalvik Executable) file. All files are
compressed in one .dex redundant
information in class file are optimized in
.dex file.
 dex file are smaller in size than class
files. The dex file resources of an
Android project are packed in “.apk”
(Android Package) file. “aapt” (Android
Asset Packaging Tool) performs this
packaging.
 Via the adb tool the necessary data in
.apk file is run and deployed to an
Android device.
 The ADT (Android Development Tools)
performs these steps transparently to the
user. If you use the ADT tooling you
press a button and a whole Android
Application is created and deployed.
Android Application Architecture
AndroidManifest.xml file:
 The Components and settings of an
Android application are described in the
AndroidManifest.xml file. It should also
contain
the
permissions
for
the
application.
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.c
om/apk/res/android"
package="edu.nmamit"
android:versionCode="1"
android:versionName="1.0">
<uses-permission
android:name="android.permission.INTERN
ET"/>
<uses-permission
android:name="android.permission.WRITE_
EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.ACCESS
_NETWORK_STATE"/>
<uses-permission
android:name="android.permission.READ_P
HONE_STATE"/>
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".HelloWorld"
android:label="@string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAI
N" />
<category
android:name="android.intent.category.L
AUNCHER" />
</intent-filter>
</activity>
<receiver
android:process=":remote"
android:name="AlarmReceiver"></receiver
>
<service android:name="MusicService"
android:enabled="true"></service>
<service android:process=":remote"
android:name=".SimpleService"
android:enabled="true"></service>
<service android:name=".MyAlarmService"
/>
<service
android:name=".AutomaticStopAlarmServic
e" />
<activity
android:name=".CloseActivity"></activit
y>
<service
android:name=".MyIntegrate"></service>
</application>
<uses-sdk android:minSdkVersion="6" />
</manifest>
 The Package attribute defines the base
package for the java objects referred to
in file. If a Java object lies within a
different package. It must be declared
with the full qualified package name
Google Play requires that every Android
Application has it own unique package.
Hence, it is good to use reverser domain
name as package name. It will avoid
confusions and Collisions with other
applications. android:versionName and
android:versionCode specify the version
of your application version name is what
the user sees and can be an integer. The
Android Market determination is based
on the version code. You start with “1”
if you need it do perform an update of
existing installations and increase the
“1” value by one if you roll-out a new
version of your application.
 The <activity> tag defines an Activity in
this example pointing to convert class in
the com.example package. Intent filter is
registered for this.
R.Java and Resource
 The gen directory is an Android project
contains the generated values R.java is a
generated class that contains references
to certain resources of the project
resources must be defined in the “res”
directory and can be XML files icons or
pictures.
 If you are creating new resources the
reference is automatically created in
R.Jave via the Eclipse ADT tools. These
are static integer values and define IDs
for the resources. In order to access the
corresponding resource via these IDs the
Android System provides methods.
 R.Java is automatically created by the
Eclipse
development
environment
manual changes are not necessary and
will be overridden by the tooling.
Assets
 The “assets” directory can be used to
store any kind of data while the “res”
directory contains
structured
values
which are known to the Android
platform. You can access this data via
the “Assets Manager” through which
you can access the “get Assets” method.
// Get the AssetManager
AssetManager manager =
getAssets();
// Read a Bitmap from Assets
try {
InputStream open =
manager.open("logo.png");
Bitmap bitmap =
BitmapFactory.decodeStream(o
pen);
// Assign the bitmap to an
ImageView in this layout
ImageView view = (ImageView)
findViewById(R.id.imageView1
);
view.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
Activities and Layouts
 Via “Layouts” the user interface for
“Activities” is defined. The layout
defines the included “Views” (Widgets)
and it properties.
 A Layout can be defined either through
java code or through XML. Most of the
since it is defined in XML file only. The
Layouts are defined via resource file in
the /res/layout folder. This specifies he
“View Groups”, Views, for this layout.
 To access a View through Java Code,
you have to give it a unique ID via the
“android:id” attribute. To assign a new
ID to a “view” use @+id/yourvalue .
<Button
android:id="@+id/cmdCalc"
android:layout_width="wrap
_content"
android:layout_height="wra
p_content"
android:text="Calculate" >
</Button>
 Layouts are defined is XML because this
separates the Logic from the layout
definition. If also allows the definitions
of different layouts. You can mix both
approaches.
Reference to resource is XML files
 In XML files you can refer to other
resource via the @ sign.
Activities and Life cycle
 The Android system Controls the life
cycle of your Application. The Android
system may stop or destroy your
application.
Through
predefined
methods the Android system defines a
life cycle “Activities” Important are:
 onSaveInstanceState() – Called after the
Activity is stopped it is used to save data
so that the “Activity ” Can restore its
states if re-started
 onPause()- always called if the Activity
ends. Can be used to release resource or
save data
 onResume() – called if the “Activity” is
restarted can be used to initialize fields.
Configuration Change
 When Configuration on change happens
an Activity will be restarted. It An event
which is relevant for the application is
triggered configuration change occurs*
for eg: if the user Changes the
orientation
vertically
of
or
the
device
horizontally)
(either
Android
assumes that an ‘Activity’ may wanted
to use different resources for those
orientations and restarts the Activity.
 In the emulator you can stimulate the
change of the orientation via Ctrl+F11
 Restart of application can be avoided by
following in case of orientation change
or position of the physical keyboard.
<activity
android:name=".ProgressTestA
ctivity"
android:label="@string/app_n
ame"
android:configChanges="orien
tation|keyboardHidden|keyboa
rd">
</activity>
How to Install Android SDK?
Eclipse
 The following assumes that you have
already java and eclipse installed and
know how to use eclipse.
Pre-requisites for using a 64bit Linux
 The Android SDK is 32bit, hence, on a
64 bit Linux system you need to have the
package ia32-libs installed. For Ubuntu
you can do this via the following
command.
Apt-get install ia32-libs
 You should check your distribution
documentation, if you are using a
different flavor of Linux
 Install ADT Plugins and Android
SDK



 Use the Eclipse update manager to
install all available components for the
Android Development Tools (ADT).
After the new Android development
components are installed you would be
prompted to install the android SDK.
Manual
Installation
of
the
Android SDK:
 After the installation of the ADT
automatically the Eclipse tooling allows
todownload the Android.
 The download contains a zip file, which
you can extract to any place is your file
system you may experience problems
with the usage of the Android SDK. So
avoid using spaces is the path name.
You should also define the location of
the
android
SDK
Preference
in
preferences
dialog
windows
–>
in
the
eclipse
via
Preferences
Eclipse
open
the
the
Menu
to
select
Android and enter the installation path of
the Android SDK.

Install a specific Android version
 The Android SDK Manager allows you
to install specific versions of Android.
 Select Windows –> Android SDK
Manager from the Eclipse menu

 The dialog allow you to install new
packages and also you to delete them.
Select “Available Packages” and open
the third party Add-Ons Select the
Google AP115 (Android 4.03) Version
of the SDK and press the install button.

Android Virtual device Emulator
What is Android Emulator?
 To run an Android system the Android
Development Tools (ADT) include an
emulator It Allows you to test your
application without having a real device.
The emulator behaves like a real device.
 Version
 Size of the SD card
 The screen resolution
 Other relevant settings
 They can be defined with different
Configuration. These devices are called
as Android Virtual Device and you can
start several in parallel.
Google vs Android AVD
 You decide whether you want an
Android device or a Google device
during the creation of an AVD. An AVD
which is created for Android will contain
the programs from the Android open
source project. An AVD, Which is
created for the Google API’s, will
contain several application and the
notable one is Google Map Application.
 Emulator Shortcuts
 To work with emulator the following are
the shortcuts.
 “Alt+Enter” maximizes the emulator.
 “Ctrl+F11” change the orientation of the
emulator
 “F8” Turns networks on/off ]
 Performance
 The native GPU of the Computer can be
used by the graphics of the emulator.
This will make the rendering in the
emulator very fast to enable this add the
GPU Emulation property to the device
configuration and set it to true.

 You can also get the “Enabled” flag for
the snapshot. This will save the state of
the emulator and will let it start much
faster. And native GPU rendering and
snapshots
do
not
work
together
currently.
 Hardware button
 Android 4.0 introduces devices that do
not have hardware any more if you want
to create such AVD add the Hardware
“Back/Home keys” Property to the
device configuration and set it to ‘false’.

 Create and run Android Virtual
Device
 To define an Android virtual Device
Define (AVD) open the AVD Manager
dialog via windows –> AVD manager
and press ”new button”.

 Enter
the
Screenshot.

values
similar
to
the
 Select the Enabled for snapshot box.
This will make the second start of the
virtual device much faster. Press the
create AVD button This will create the
AVD Configuration and display it under
the virtual devices.
 Select your devices and press”start”
button to test your setup is correct.
 The
AVD
start
after
sometime
Interputting this process might correct
the AVD. You can use the AVD via the
mouse and he virtual keyboard of the
emulator after the AVD started.
 How to make an android Application?
 Install the demo application
 This application is also available on the
Android Market place. To install if via
the google play application you can also
scan
following
barcode
with
your
android Smartphone alternatively.
 Create Projects
 Select file –> New–> other–> Android
Project and create the Android Project
enter the following.



 Press ‘Finish’ this should create the
following directory structure.

 Modifying resource
 ADT provide specialized editor for
resource files as described in the android
Development Tools. These allows to
between XML representation of file and
rich user interface through tabs on the
button of the editor.
 Create attributes
 Android allows you to create static
attributes. These attributes can for
example be used in your XML layout
files or referred to via Java source code.
 Select
the
file
“res/values/string
/CXML” and press the ‘Add’ button
select ‘color’ and enter my color as the
name and “3399CC” as the value.

 Add the following “string” attributes
string attributes allow the developer to
translate the application at a later point.
 Table 1: String Attributes
 Name
 Value
 celsius
 to Celsius
 fahrenheit
 to
Fahrenheit
 calc
 Calculate
 Switch to the XML representation and
validate that the values are correct.
 Hello
Temperature
#3399CC
World,
Convert!
Converter
myClickHandler
to
Celsius
to
Fahrenheit
Calculate
 Add Views
 Select “res/layout/main.XML/” and open
the Android editor via a double click.
This editor allows you to create the
layout via drag and drop or via the xml
source code. You can switch between
both representation via the tabs at the
bottom of the editor for changing the
position and grouping elements you can
use the eclipse “Outline” View.
 The
following
screenshot
of
the
“palette” view from which you can drag
and drop new user interface components
into your layout. Note that the”Palette”
view changes frequently so your view
might be a bit different.

 You will now create your new layout to
remove the text object right click on the
existing text object “Hello World Hello”
in the layout select “delete” from the
popup menu. Then from the “Palette”
view select Text fields and locate “Text
fields” derive from the class “Edit text”
they just specify via an additional
attributes which text type can be used.
 Then select the Palette section “Form
widgets” and drag a “Radio Group”
Object onto the layout. The number of
radio button added to the radio button
group depends on your version of
Eclipse. Make sure there are two radio
buttons by deleting or adding radio
buttons to the group.
 From the palette section from widgets
drag a button object onto the layout. The
result should look as follows.

 Switch to ’main.xml’and verify that
your xml looks like the following.
 <?xml
version=”1.0″
encoding=”utf-
8″?>
 <LinearLayout
xmlns:android=”http://schemas.android.
com/apk/res/android”
 android:layout_width=”fill_parent”
 android:layout_height=”fill_parent”
 android:orientation=”vertical” >
 <EditText
 android:id=”@+id/editText1″
 android:layout_width=”match_parent”
 android:layout_height=”wrap_content”
 android:text=”EditText” >
 </EditText>
 <RadioGroup
 android:id=”@+id/radioGroup1″
 android:layout_width=”match_parent”
 android:layout_height=”wrap_content”
>
 <RadioButton
 android:id=”@+id/radio0″
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:checked=”true”
 android:text=”RadioButton” >
 </RadioButton>
 <RadioButton
 android:id=”@+id/radio1″
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:text=”RadioButton” >
 </RadioButton>
 </RadioGroup>
 <Button
 android:id=”@+id/button1″
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:text=”Button” >
 </Button>
 </LinearLayout>
 Edit View properties
 You change user interface components
properties via the eclipse ‘properties’
view if you select it most of the
properties can be changed via the right
mouse menu you can also edit properties
of field directly in xml If you know what
you want you to change, changing
properties in the XML file is much
faster. If you are searching for a certain
properties the right mouse functionality
is rice.
 Open your main.xml layout file the edit
text control shows currently a default
text you need to delete it in the xml
code. Switch to the xml tab called
‘main.xml’
and
delete
the
android:text=”EditText” property from
the edit text part switch back to the
Graphical layout tab and check that the
text is removed.
 Use the right mouse click on the first
radio button to assign the “Celsius”
string attribute to its “text” property
Assign the “fahrenheit” string to the
second radio button.


 You can always either edit the xml file
or modify the properties via right mouse
click.
 Set the property”Checked” to true for the
first Radio Button. Assign “Calc” to text
property of your button and assign
“myClickHandler” to the ‘on click’
property.
 Set the “Input type” Property to “number
signed” and “numberDecimal” on your
edit text.
 In a Linear layout all your user Interface
Components are contained you need to
assign a background color to this
“LinearLayout” Right click on an empty
space in graphical layout mode then
select other properties–>All by name –>
Background select ‘color’ and then
select ‘mycolor’ in the list which is
displayed.

 Switch to the main.xml tab and verify
that the xml is correct.
 <?xml
8″?>
version=”1.0″
encoding=”utf-
 <LinearLayout
xmlns:android=”http://schemas.android.
com/apk/res/android”
 android:layout_width=”fill_parent”
 android:layout_height=”fill_parent”
 android:background=”@color/myColor”
 android:orientation=”vertical” >
 <EditText
 android:id=”@+id/editText1″
 android:layout_width=”match_parent”
 android:layout_height=”wrap_content”
 android:inputType=”numberDecimal|nu
mberSigned” >
 </EditText>
 <RadioGroup
 android:id=”@+id/radioGroup1″
 android:layout_width=”match_parent”
 android:layout_height=”wrap_content”
>
 <RadioButton
 android:id=”@+id/radio0″
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:checked=”true”
 android:text=”@string/celsius” >
 </RadioButton>
 <RadioButton
 android:id=”@+id/radio1″
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:text=”@string/fahrenheit” >
 </RadioButton>
 </RadioGroup>
 <Button
 android:id=”@+id/button1″
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:onClick=”myClickHandler”
 android:text=”@string/calc” >
 </Button>
 </LinearLayout>
 Change the Activity source Code
 You specified that an “activity” called
“convert activity” should be created
during the, generation of view Android
project. The project wizard created based
on the ‘onclick’ property of your button.
 package de.vogella.android.temperature;
 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.RadioButton;
 import android.widget.Toast;
 public class ConvertActivity extends
Activity {
 private EditText text;
 @Override
 public
void
onCreate(Bundle
savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 text
=
(EditText)
findViewById(R.id.editText1);
}
 // This method is called at button click
because we assigned the name to the
 // “On Click property” of the button
 public
void
myClickHandler(View
view) {
 switch (view.getId()) {
 case R.id.button1:
 RadioButton
celsiusButton
=
(RadioButton)
findViewById(R.id.radio0);
 RadioButton
fahrenheitButton
=
(RadioButton)
findViewById(R.id.radio1);
 if (text.getText().length() == 0) {
 Toast.makeText(this, “Please enter a
valid number”,
 Toast.LENGTH_LONG).show();
 return;
}
 float
inputValue
=
Float.parseFloat(text.getText().toString()
);
 if (celsiusButton.isChecked()) {
 text.setText(String
 .valueOf(convertFahrenheitToCelsius(in
putValue)));
 celsiusButton.setChecked(false);
 fahrenheitButton.setChecked(true);
 } else {
 text.setText(String
 .valueOf(convertCelsiusToFahrenheit(in
putValue)));
 fahrenheitButton.setChecked(false);
 celsiusButton.setChecked(true);
}
 break;
}
}
 // Converts to celsius
 private
float
convertFahrenheitToCelsius(float
fahrenheit) {
 return ((fahrenheit – 32) * 5 / 9);
}
 // Converts to fahrenheit
 private
convertCelsiusToFahrenheit(float
celsius) {
 return ((celsius * 9) / 5) + 32;
}
float
}
 Start Project
 To start the application select your
project right click on it and select Run–
>Android application If an emulator
starts up very slowly.
 You should get the following result

 Type
in
a
number,
conversion and p
select
your