Android Programming ECE 455/555 Embedded System Design Wei Gao

advertisement
ECE 455/555
Embedded System Design
Android Programming
Wei Gao
Fall 2015
1
Fundamentals of Android Application
 Java programming language
 Code along with any required data and resource files are
compiled into an Android package, .apk file.
 Android applications don't have a single entry point
for everything in the application
 No main() function
 Default entry when you click the application icon
 Components can also be started by other applications
 http://developer.android.com/guide/topics/fundame
ntals.html
ECE 455/555 Embedded System Design
2
Activating Components
 Application components
 Activities, services, broadcast receivers
 Activated by an asynchronous message called intent
 Bind components at runtime
 You can start an activity by passing an intent to
startActivity()
 Specify the main entry of your program in this way!
 You can start a service by passing an intent to
startService()
ECE 455/555 Embedded System Design
3
Analyzing HelloWorld Application
One of the standard entry
function: crate an activity
instance
Create an user interface and
set the text
Default function to setup the
user menu specified in class R
ECE 455/555 Embedded System Design
4
Manifest XML file
 Applications must declare their components in a
manifest file
 Before Android can start an application component, it
must learn that the component exists.
 AndroidManifest.xml for each application
 Declares the application's components
 Names any libraries the application needs to be linked
against
 Identifies any permissions the application expects to be
granted
ECE 455/555 Embedded System Design
5
The Manifest XML File of HelloWorld
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
The only
component
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Similarly you can claim <service>, <receiver> and
</manifest>
<provider> within the <application> tag
ECE 455/555 Embedded System Design
6
The Manifest XML File of HelloWorld
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
The platform
version used
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
Being indicated as
android:label="@string/title_activity_main" >
application entry
<intent-filter>
<action android:name="android.intent.action.MAIN" />
of
Type
action
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Other action types: CALL, SYNC, BATTERY_LOW,etc
</application>
</manifest>
http://developer.android.com/guide/components
/intents-filters.html
ECE 455/555 Embedded System Design
7
More in the Manifest XML file
 Application requirements
 Using external sensor/devices
<uses-feature android:name="android.hardware.bluetooth" />
<uses-feature android:name="android.hardware.camera" />
 Using Android library
<uses-library android:name="android.view.animation" />
 Request permission
<uses-permission android:name="android.permission.CAMERA" />
 More details at:
http://developer.android.com/guide/topics/manifest/manifes
t-intro.html
ECE 455/555 Embedded System Design
8
API Demos
 Demonstrating how the
system API work
 Import demo projects into
Eclipse
 -> Android project from
existing code
 \...\Android\androidsdk\samples\android10\ApiDemos
ECE 455/555 Embedded System Design
9
API Demos
 Run the program as a Java application
 It will automatically compile and upload the .apk file to the
phone
 There will be an application named
“API Demos” on your phone
 You will see something looks like:
ECE 455/555 Embedded System Design
10
Sensing API Demo
 In the API Demo menu, choose OS->Sensors
 Application that displays the values of 3-axis acceleration
sensors graphically
 Frequently used in games for
gravity detection!
 implementation
 com.example.android.apis.os
/Sensors.java
ECE 455/555 Embedded System Design
11
Sensing API Demo
 Access sensor readings through SensorManager class
Get an instance of SensorManager
Listen sensor readings when the
application is active at foreground
Release listening
ECE 455/555 Embedded System Design
12
Sensing API Demo
 Read and convert sensor readings
Implemented as an
event handler
Readings of 3D
orientation sensors
Otherwise, use
accelerator data
Reading conversions
ECE 455/555 Embedded System Design
13
Sensing Related Packages
 Media
 http://developer.android.com/reference/android/media/p
ackage-summary.html
 Location
 http://developer.android.com/reference/android/location/
package-summary.html
 Hardware
 http://developer.android.com/reference/android/hardwar
e/Sensor.html
ECE 455/555 Embedded System Design
14
Thoughts…
 Can you realize fancier functionalities based on the
sensing capabilities provided by Android?
 All the codes you need are included in the samples!
 Check \...\Android\android-sdk\samples\android-10\
 Integrating sensing capabilities with other
applications
 SMS, GPS, camera, etc
ECE 455/555 Embedded System Design
15
Summary
 Android: an open-source operating system for
smartphones
 Micro-kernel, middleware-based OS implementation
 Each application runs in Java virtual machine
• Expensive but reliable and secure
 Application
 Java programming enabled
 Components: activity, service, content provider, broadcast
receiver
 Configured by Manifest XML file
 Sensing capabilities of smartphones
 Enable more flexible software design
ECE 455/555 Embedded System Design
16
Other Smartphone Platforms
 Doing your project on your own smartphones or
tablets
 iPhone, BlackBerry, iPad, etc.
 A tutorial of iPhone programming can be found here.
 http://www.edumobile.org/iphone/category/iphoneprogramming-tutorials/
 http://web.eecs.utk.edu/~weigao/ece455/fall2015/iPhone
Development.zip
ECE 455/555 Embedded System Design
17
Download