420 likes | 429 Views
Learn the basics of Android application development, including Java programming, activity lifecycle, user interface design, and using intents, broadcasts, and notifications.
E N D
Android Jump Start: Application Development Marcus Chou
Market trend Close to 20% of U.S. smartphone owners spent $100 on apps in 2008 -- ABI Research Survey
Smart phone of nowadays Blonde??
Prerequisites • Java programming skills • Android SDK
Java • Simple, Object Oriented, and familiar • Interface, Abstract, extends, implement Reference:http://java.sun.com/docs/books/tutorial/
Android SDK • Emulator • Tools • Document • Library • Sample code Reference:http://code.google.com/android/documentation.html
Four building block... • Activity • Broadcast Intent Receiver • Service • Content Provider
Activity • A activity is usually a single screen in an application • A user interface compsed of Views and respond to events • A application may consist of many activities
Activity (cont.) Resource • Drawable • Layout • Values
Activity (cont.) UI Layout Design <TabHost> <LinearLayout> <LinearLayout> <ImageView/> <ScrollView> <TextView/> </ScrollView> </LinearLayout> <LinearLayout> <TabWidget/> <FrameLayout/> </LinearLayout> </LinearLayout></TabHost>
Activity (cont.) public class About extends Activity{ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.about); String about = this.getIntent().getExtras().getString("about"); TextView tv=(TextView)this.findViewById(R.id.about); tv.setText(about); }}
Activity (cont.) • Use "Intent" to activate the screen (Activity) • The two most important parts of the intent data structure are the action and the data to act upon.
Explicit Intents (cont.) Intent intent = new Intent(ItemList.this,VideoPlayer.class); intent.putExtra("file_path", selectedItem.getFilePath()); startActivity(intent);
Implicit Intents Uri uri = Uri.parse("http://tw.youtube.com/watch?v=S6o7rwrxt4g"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); try { if (startActivity(intent)) { // success } } catch (ActivityNotFoundException ex) { // fail }
Broadcast Intent Receiver • Broadcast Receiver • Notification
Broadcast Receiver (cont.) private void registerBroadcastListener(){ IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK ); this.registerReceiver(this.mBlackdidiListener, filter);} private class BlackdidiBroadcastListener extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { startActivity((new Intent(BlackdidiSample.this, About.class)).putExtra("about", "time tick")); }}
Broadcast Receiver (cont.) <receiver android:name="com.blackdidi.comp.BootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver><receiver android:name="com.blackdidi.comp.ConnectivityReceiver"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
Notification (cont.) public class BlackdidiSample extends Activity { private NotificationManager mNM; ... public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); ... } ... }
Notification (cont.) private void showNotification() { CharSequence text = getText(R.string.hello_notification); Notification notification = new Notification(R.drawable.star_big_on, text, System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0,( new Intent(this, About.class)).putExtra("about", "yoho, blackdidi"), 0); notification.setLatestEventInfo(this, getText(R.string.hello_notification),text, contentIntent); mNM.notify(R.string.app_name, notification); }
Service • A Service is code that is long-lived and runs without a UI • Use Context.bindService() to connect to the service
Service (cont.) <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0.0" package="com.blackdidi.podcast"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <service android:name=".xml.ChannelService" /> <service android:name=".download.DownloadService" /> ....
Content Provider • A content provider is only required if you need to share data between multiple applications. • Querying for Data • Modifying Data • Adding a Record • Deleting a Record
Content Provider (cont.) public class BlackdidiList extends ListActivity{ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null); startManagingCursor(c); ListAdapter adapter= new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, c, new String[] {CallLog.Calls.NUMBER , CallLog.Calls.CACHED_NAME }, new int[] { android.R.id.text1, android.R.id.text2 }); setListAdapter(adapter); }}
Security <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission><uses-permission android:name="android.permission.WAKE_LOCK"> </uses-permission><uses-permission android:name="android.permission.BATTERY_STATS"> </uses-permission>
Reference Android Developers Site http://developer.android.com/ Android Developers Blog http://developer.android.com/ Android Developers Group http://groups.google.com/group/android-developers Android Platform Discussion Group http://groups.google.com/group/android-platform