View
2.084
Download
3
Category
Tags:
Preview:
Citation preview
Android Jump Start
By Pune Google Technology User Group
(Pune-GTUG)
Topics
• Android Basics• Android Building Blocks• Building Application
Android Basics
• Android is a software stack for mobile devices that includes an operating system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
Android Basics
Features• Application framework enabling reuse and replacement of components• Dalvik virtual machine optimized for mobile devices• Integrated browser based on the open source WebKit engine• Optimized graphics powered by a custom 2D graphics library; 3D graphics
based on the OpenGL ES 1.0 specification (hardware acceleration optional)• SQLite for structured data storage• Media support for common audio, video, and still image formats (MPEG4,
H.264, MP3, AAC, AMR, JPG, PNG, GIF)• GSM Telephony (hardware dependent)• Bluetooth, EDGE, 3G, and WiFi (hardware dependent)• Camera, GPS, compass, and accelerometer (hardware dependent)• Rich development environment including a device emulator, tools for
debugging, memory and performance profiling, and a plugin for the Eclipse IDE
Android Basics
Topics
• Android Basics• Android Building Blocks• Building Application
Building Blocks
Activity ServiceBroadcastReceiver
Content Provider
BackgroundProcess♫♪♫♪♫
Data Store(Playlist)
Communication is using Intents
Phone Call Comes
Topics
• Android Basics• Android Building Blocks• Building Application
Building Application .
Lets build a new Music Player
Building Application .
• Step 1 – You need a Screen (Activity)
Activity
Views Layouts
Views could be• Buttons• Text Views• etc ….
Layouts could be• Linear Layout• Relative Layout• Table Layout• etc …..
Building Application .
• Activity Life Cycle
ForegroundLifeCycle
Visible LifeCycle
Complete LifeCycle
Building Application .
• Activity Life Cycle made easier
onCreate
onDestroy
onStart
onStop
onResume
onPause
Building Application .
• What to do in each life cycle methods?
onCreate
onDestroy
onStart
onStop
onResume
onPause
Building Application .
• Lets Draw the Screen using Linear Layouts
Current Playlist
Player
Building Application .
• Lets Draw the Screen using Linear Layouts
Current Music Info
Progress Bar
Buttons
Music file 1
Music file 2
Music file 3
Music file 4
Building Application .
• Lets Draw the Screen using Linear Layouts
Current Music Info
Progress Bar
Music file 1
Music file 2
Music file 3
Music file 4
Prev NextPlay
Building Application .
• Lets Draw the Screen using Relative Layouts
Beautiful World
Take That
Title is aligned to the top and right of the image
Author is aligned to the bottom and right of the image
Building Application .
• Use Layouts– Layouts can be defined in different XML files– Code can refer to these layout xml files
Building Application .• Use Layouts
Building Application .• Use Layouts
Building Application .• Use Layouts
Building Application .
• Lets write code to play an mp3 file
MediaPlayer mp = new MediaPlayer();
public void play(){
mp.setDataSource(PATH_TO_FILE);
mp.prepare();
mp.start();
}
Building Application .• Lets attach this code to the button
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button playButton = (Button)findViewById(R.id.button_play);
playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View arg0)
{
play();
}
});
}
Building Application .
• Refer following for detailed instructions http://www.helloandroid.com/node/134
Problem with Playing .in Activity
• Phone Calls• Pressing Back Button
Will kill you Activity and your Music will stop playing
Building Application .
• Step 2 – Delegate Playing to a Service1. Define an MusicPlayer.aidl with methods
2. Autogenerates MusicPlayer Interface
3. Extend Service Implement MusicPlayer Stub
4. Declare Service in Manifest.xml
5. Create a ServiceConnection Object
6. Bind to Service
7. Access the service methods
Building Application .
Service
LifeCycle
Building Application .Service lifecycle (Note)A service can be used in two ways:• It can be started and allowed to run until someone stops it or it stops itself. In this mode, it's started
by calling Context.startService() and stopped by callingContext.stopService(). It can stop itself by calling Service.stopSelf() or Service.stopSelfResult(). Only one stopService() call is needed to stop the service, no matter how many times startService() was called.
• It can be operated programmatically using an interface that it defines and exports. Clients establish a connection to the Service object and use that connection to call into the service. The connection is established by calling Context.bindService(), and is closed by calling Context.unbindService(). Multiple clients can bind to the same service. If the service has not already been launched, bindService() can optionally launch it.
The two modes are not entirely separate. You can bind to a service that was started with startService(). For example, a background music service could be started by calling startService() with an Intent object that identifies the music to play. Only later, possibly when the user wants to exercise some control over the player or get information about the current song, would an activity establish a connection to the service by calling bindService(). In cases like this, stopService() will not actually stop the service until the last binding is closed.
Like an activity, a service has lifecycle methods that you can implement to monitor changes in its state. But they are fewer than the activity methods — only three — and they are public, not protected:
• void onCreate() • void onStart(Intent intent)• void onDestroy()
Building Application .
• Create an aidl Interface
Building Application .
• Extend Service, Implement Stub
Building Application .
• Declare in Manifest.xml
• In order to use a remote service first we must add a line to our AndroidManifest.xml file inside our application tag to define our service, here is that line:
<service class=".MusicService" android:process=":remote" />
Building Application .
• Refer following for detailed instructions http://www.helloandroid.com/node/140
Building Application .
• Step 3 – Reuse using Content Provider
ExistingPlaylist
On ExistingMusic Player
Old Music Player New Music Player
Building Application .
• Step 3 – Reuse using Content Provider
ExistingPlaylist
On ExistingMusic Player
Old Music Player New Music Player
?
Building Application .
• Content Resolver & Content Providers
ContentProvider
ContentResolver
URI of Provider
CursorApplication
ContentProvider
Application
ContentProvider
Application
ContentProvider
Application
ContentProvider
URI matches
Building Application .
• Content Resolver & Content Providers
ContentProvider
• Extends ContentProvider• More like a restful Service• Has a Unique URI• CRUD Methods
Building Application .
Content Resolver & Content Providers
String[] projection = new String[] { People._ID, People._COUNT, People.NAME, People.NUMBER };
Uri contacts = People.CONTENT_URI;
Cursor managedCursor = managedQuery(contacts, projection,
// Which columns to return
null,
// Which rows to return (all rows)
null,
// Selection arguments (none)
// Put the results in ascending order by name
People.NAME + " ASC");
Building Application .
Content Resolver & Content Providers
private void getColumnData(Cursor cur){
if (cur.moveToFirst()) {
String name;
String phoneNumber;
int nameColumn = cur.getColumnIndex(People.NAME);
int phoneColumn = cur.getColumnIndex(People.NUMBER);
String imagePath;
do {
// Get the field values
name = cur.getString(nameColumn);
phoneNumber = cur.getString(phoneColumn);
// Do something with the values. ...
} while (cur.moveToNext());
}
}
• http://developer.android.com/guide/topics/providers/content-providers.html
Building Application .
• Step 4 – Keeping informed - Broadcast Receivers
Phone Call Comes
Pause the Music Player
Phone rings
Conversationends
Building Application .
• Understanding Intents
Intents
Explicit Implicit
Need• Class Name
Need • ACTION• CATEGORY• DATA
Building Application .
• Understanding Intents
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sample“ android:versionCode="1“ android:versionName="1.0"> <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.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="4" /></manifest>
Program launcher shows all the activities which have MAIN Action and LAUNCHER category
Building Application .
• Understanding Intents
So what happens two activities have the exact same intent filter and an intent is fired.
Simple you choose one application, and you have an option to tell to se that application as the default application hence forth
Building Application .
• Understanding Intents
Intent to launch an Activity
• Context.startActivity(intent)
• ContextstartActivityForResult(intent)
Intent to launch an Service
• Context.startService(intent)
Intent to send a broadcast
• Context.sendBroadCast(intent)
Building Application .
• Step 4 – Keeping informed - Broadcast Receivers– Add BroadCast Receiver– Check Phone status and pause or play the
music using service
• <receiver android:name=".PhoneReceiver" android:enabled="true"> <intent-filter> <action android:name= "android.intent.action.PHONE_STATE" /> </intent-filter> </receiver>
Building Application .
• Step 4 – Keeping informed - Broadcast Receivers
public class Receiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intentAction.equals(ACTION_PHONE_STATE_CHANGED)) { pstn_state = intent.getStringExtra("state"); //Depending on the pstn state pause or play music using the
//MusicService
}
}
}
Recommended