basics of android
TRANSCRIPT
What is Android
• Android is an open source operating system, created by Google specifically for use on mobile devices (cell phones and tablets)
• Linux based (2.6 kernel)
• Can be programmed in C/C++ but most app development is done in Java (Java access to C Libraries via JNI (Java Native Interface))
• Supports Bluetooth, Wi-Fi, and 3G and 4G networking
Android development
Android Manifest
Resource XML
Java Source
Generated Class
Java Compiler
Android Libraries
.dexFile
DalvikVM
WHAT IS ANDROID?
A Software platform and operating system
for mobile.
Based on the Linux kernel.
Android was found way back in 2003.
It was developed in Palo Alto, California.
Android was developed by the Andy
Rubin, Rich Miner, Nick Sears and Chris
White.
Android was purchased by the GOOGLE
in AUGUST,2005 for 50 million $.
First Version of Android. The focus of Android beta is testing incorporating usability. Android beta will generally have many more problems on speed and
performance.
First full version of android. Released on September 23, 2008. Wi-Fi and Bluetooth support. Quite slow in operating. copy and paste feature in the web browser is
not present.
Released on April 30, 2009. Added auto-rotation option. Copy and Paste feature added in the web
browser. Increased speed and performance but not upto
required level.
Released on September 15, 2009. Voice search and Search box were added. Faster OS boot times and fast web browsing
experience. Typing is quite slower.
Released on October 26, 2009. Bluetooth 2.1 support. Improved typing speed on virtual
keyboard, with smarter dictionary. no Adobe flash media support.
Released on May 20, 2010. Support for Adobe Flash 10.1 Improved Application launcher with better browser No internet calling.
Released on December 6, 2010. Updated User Interface with high efficiency and speed Internet calling One touch word selection and copy/paste. New keyboard for faster word input. More successful version of Android than previous
versions. not supports multi-core processors.
Released on February 22, 2011. Support for multi-core processors Ability to encrypt all user data. This version of android is only available for
tablets.
Released on November 14, 2011. Virtual button in the UI. A new typeface family for the UI, Roboto. Ability to shut down apps that are using data in the
background.
Released on June 27, 2012. Latest version of Android. Smoother user interface.
Making source code available to everyone inevitably invites the attention of hackers.
Android operating system uses more amount of battery as compared to normal mobile phones.
As there are so many user sometimes it becomes difficult to connect all the users.
As we call Android is world of applications we continuously need to connected with the internet which is not possible for all the users.
Android is now stepping up in next level of mobile internet.
There are chances of Android Mobile sales becomes more then iPhone in next two years.
Google may launch another version of android that starts K because Google is launching all the android versions in the alphabetical order.
There are chances of Android may become the widely used operating system in world.
@2011 Mihail L. Sichitiu 13
Applications
• Written in Java (it’s possible to write native code – will not cover that here)
• Good separation (and corresponding security) from other applications:
– Each application runs in its own process
– Each process has its own separate VM
@2011 Mihail L. Sichitiu 14
Application Components
• Activities – visual user interface focused on a single thing a user can do
• Services – no visual interface – they run in the background
• Broadcast Receivers – receive and react to broadcast announcements
• Content Providers – allow data exchange between applications
@2011 Mihail L. Sichitiu 15
Activities
• Basic component of most applications
• Most applications have several activities that start each other as needed
• Each is implemented as a subclass of the base Activity class
@2011 Mihail L. Sichitiu 16
Activities – The View
• Each activity has a default window to draw in (although it may prompt for dialogs or notifications)
• The content of the window is a view or a group of views (derived from View or ViewGroup)
• Example of views: buttons, text fields, scroll bars, menu items, check boxes, etc.
• View(Group) made visible via Activity.setContentView() method.
@2011 Mihail L. Sichitiu 17
Services
• Does not have a visual interface
• Runs in the background indefinitely
• Examples
– Network Downloads
– Playing Music
– TCP/UDP Server
• You can bind to a an existing service and control its operation
@2011 Mihail L. Sichitiu 18
Broadcast Receivers
• Receive and react to broadcast announcements
• Extend the class BroadcastReceiver
• Examples of broadcasts:
– Low battery, power connected, shutdown, timezone changed, etc.
– Other applications can initiate broadcasts
@2011 Mihail L. Sichitiu 19
Content Providers
• Makes some of the application data available to other applications
• It’s the only way to transfer data between applications in Android (no shared files, shared memory, pipes, etc.)
• Extends the class ContentProvider;
• Other applications use a ContentResolver object to access the data provided via a ContentProvider
@2011 Mihail L. Sichitiu 20
Intents
• An intent is an Intent object with a message content.
• Activities, services and broadcast receivers are started by intents. ContentProviders are started by ContentResolvers:– An activity is started by Context.startActivity(Intent intent) or
Activity.startActivityForResult(Intent intent, int RequestCode)
– A service is started by Context.startService(Intent service)
– An application can initiate a broadcast by using an Intent in any of Context.sendBroadcast(Intent intent), Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast()
@2011 Mihail L. Sichitiu 21
Shutting down components
• Activities– Can terminate itself via finish();
– Can terminate other activities it started via finishActivity();
• Services– Can terminate via stopSelf(); or Context.stopService();
• Content Providers– Are only active when responding to ContentResolvers
• Broadcast Receivers– Are only active when responding to broadcasts
@2011 Mihail L. Sichitiu 22
Android Manifest
• Its main purpose in life is to declare the components to the system:
<?xml version="1.0" encoding="utf-8"?><manifest . . . >
<application . . . ><activity android:name="com.example.project.FreneticActivity"
android:icon="@drawable/small_pic.png"android:label="@string/freneticLabel" . . . >
</activity>. . .
</application></manifest>
Manifest File (1)
• Contains characteristics about your application
• When have more than one Activity in app, NEED to specify it in manifest file
– Go to graphical view of the manifest file
– Add an Activity in the bottom right
– Browse for the name of the activity
• Need to specify Services and other components too
• Also important to define permissions and external libraries, like Google Maps API
@2011 Mihail L. Sichitiu 25
Intent Filters
• Declare Intents handled by the current application (in the AndroidManifest):
<?xml version="1.0" encoding="utf-8"?><manifest . . . >
<application . . . ><activity android:name="com.example.project.FreneticActivity"
android:icon="@drawable/small_pic.png"android:label="@string/freneticLabel" . . . >
<intent-filter . . . ><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />
</intent-filter><intent-filter . . . >
<action android:name="com.example.project.BOUNCE" /><data android:mimeType="image/jpeg" /><category android:name="android.intent.category.DEFAULT" />
</intent-filter></activity>. . .
</application></manifest>
Shows in the Launcher and is the main activity to start
Handles JPEG images in some way
References
• This tutorial is a brief overview of some major concepts…Android is much richer and more complex
• Developer’s Guide– http://developer.android.com/guide/index.html
• API Reference– http://developer.android.com/reference/packages.html
Tools
• Phone• Eclipse ( http://www.eclipse.org/downloads/ )
– Android Plugin (ADT)
• Android SDK ( http://developer.android.com/sdk/index.html )– Install everything except Additional SDK Platforms,
unless you want to
• Windows Users: may need to install Motorola Driver directly ( http://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-
Drivers )
Android SDK
• Once installed open the SDK Manager
• Install the desired packages
• Create an Android Virtual Device (AVD)
ADT Plugin (1)
• In Eclipse, go to Help -> Install New Software
• Click ‘Add’ in top right
• Enter:– Name: ADT Plugin
– Location: https://dl-ssl.google.com/android/eclipse/
• Click OK, then select ‘Developer Tools’, click Next
• Click Next and then Finish
• Afterwards, restart Eclipse
• Specify SDK location (next 3 slides)– Must do this every time start a new project in a new location (at
least in Windows)
Project Components
• src – your source code
• gen – auto-generated code (usually just R.java)
• Included libraries
• Resources
– Drawables (like .png images)
– Layouts
– Values (like strings)
• Manifest file
XML
• Used to define some of the resources– Layouts (UI)– Strings
• Manifest file• Shouldn’t usually have to edit it directly,
Eclipse can do that for you• Preferred way of creating UIs
– Separates the description of the layout from any actual code that controls it
– Can easily take a UI from one platform to another
R Class
• Auto-generated: you shouldn’t edit it
• Contains IDs of the project resources
• Enforces good software engineering
• Use findViewById and Resources object to get access to the resources
– Ex. Button b = (Button)findViewById(R.id.button1)
– Ex. getResources().getString(R.string.hello));
Layouts (1)
• Eclipse has a great UI creator
– Generates the XML for you
• Composed of View objects
• Can be specified for portrait and landscape mode
– Use same file name, so can make completely different UIs for the orientations without modifying any code
Layouts (3)
• Click ‘Create’ to make layout modifications
• When in portrait mode can select ‘Portrait’ to make a res sub folder for portrait layouts
– Likewise for Landscape layouts while in landscape mode
– Will create folders titled ‘layout-port’ and ‘layout-land’
• Note: these ‘port’ and ‘land’ folders are examples of ‘alternate layouts’, see here for more info– http://developer.android.com/guide/topics/resources/providing-resources.html
• Avoid errors by making sure components have the same id in both orientations, and that you’ve tested each orientation thoroughly
Strings
• In res/values– strings.xml
• Application wide available strings• Promotes good software engineering• UI components made in the UI editor should
have text defined in strings.xml
• Strings are just one kind of ‘Value’ there are many others
Android Programming Components
• Activity– http://developer.android.com/guide/topics/fundamentals/activities.html
• Service– http://developer.android.com/guide/topics/fundamentals/services.html
• Content Providers
• Broadcast Receivers
• Android in a nutshell:– http://developer.android.com/guide/topics/fundamentals.html
Activities (1)
• The basis of android applications
• A single Activity defines a single viewable screen– the actions, not the layout
• Can have multiple per application
• Each is a separate entity
• They have a structured life cycle– Different events in their life happen either via the
user touching buttons or programmatically
Services (1)
• Run in the background
– Can continue even if Activity that started it dies
– Should be used if something needs to be done while the user is not interacting with application• Otherwise, a thread is probably more applicable
– Should create a new thread in the service to do work in, since the service runs in the main thread
• Can be bound to an application
– In which case will terminate when all applications bound to it unbind
– Allows multiple applications to communicate with it via a common interface
• Needs to be declared in manifest file
• Like Activities, has a structured life cycle
Running in Eclipse (1)
• Similar to launching a regular Java app, use the launch configurations
• Specify an Android Application and create a new one
• Specify activity to be run• Can select a manual option, so each time
program is run, you are asked whether you want to use the actual phone or the emulator– Otherwise, it should be smart and use whichever
one is available
Debugging• Instead of using traditional System.out.println, use the Log class
– Imported with android.util.Log– Multiple types of output (debug, warning, error, …)– Log.d(<tag>,<string>)
• Can be read using logcat.– Print out the whole log, which auto-updates
• adb logcat
– Erase log• adb logcat –c
– Filter output via tags• adb logcat <tag>:<msg type> *:S• can have multiple <tag>:<msg type> filters• <msg type> corresponds to debug, warning, error, etc.• If use Log.d(), then <msg type> = D
• Reference– http://developer.android.com/guide/developing/debugging/debugging-log.html
Android Event HandlersFrom the code file for the activity:
Button ok = (Button) findViewById(R.id.button1);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CharSequence s = et.getText();
tv.setText("Welcome, " + s);
}
});