Successfully reported this slideshow.

Android app deveopment

2,188 views

Published on

Published in: Design, Technology
  • Be the first to comment

  • Be the first to like this

Android app deveopment

  1. 1. Android App Development
  2. 2. 1.Notifying the users Debugging android applications Intent and intent filters2.Multimedia in android Multimedia supported audio formats Simple media playback Supported video formats3.sql database Introducing sqlite Sqlite open helper and creating a database Opening and closing a database4.Basic content providers Content provider mime types Searching for content Adding, changing, and removing content
  3. 3. 5.Graphics and Animations Drawing graphics in android Drawing with xml Canvas drawing best practice6.Accessing android hardware Using the media apis Using the camera Using the accelerometer and compass
  4. 4. 1.Notifying the users Debugging android applications Intent and intent filters2. Multimedia in android Multimedia supported audio formats Simple media playback Supported video formats
  5. 5. NotificationsThere are Basically three kinds of Notifications in android:1. Toast Notification2. StatusBar Notifications3. Dialog Notifications
  6. 6. Notification : Toast A toast notification is a message that pops up on the surface of the window. The notification automatically fades in and out, and does not accept interaction events. A toast is best for short text messages, such as "File saved," when youre fairly certain the user is paying attention to the screen.
  7. 7. Toast: Code SnippetContext context = getApplicationContext();CharSequence text = "Hello toast!";int duration = Toast.LENGTH_SHORT;Toast toast = Toast.makeText( context , text , duration);toast.show(); (OR)Toast.makeText(context, text, duration).show();Reference : http://developer.android.com/guide/topics/ui/notifiers/toasts.html
  8. 8. Positioning the Toast:public void setGravity (int gravity, int xOffset, int yOffset)This Sets the location at which the notification should appear on the screen.
  9. 9. Status Bar Notification A status bar notification adds an icon to the systems status bar (with an optional ticker-text message) and an expanded message in the "Notifications" window. When the user selects the expanded message, Android fires an Intent that is defined by the notification (usually to launch an Activity).
  10. 10. Status Bar Notification : code snippet Get a reference to the NotificationManager:String ns = Context.NOTIFICATION_SERVICE;NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); Instantiate the Notification:int icon = R.drawable.notification_icon;CharSequence tickerText = "Hello";long when = System.currentTimeMillis();Notification notification = new Notification(icon, tickerText, when);
  11. 11. Status Bar Notification : code snippet Define the notifications message and PendingIntent:Context context = getApplicationContext();CharSequence contentTitle = "My notification";CharSequence contentText = "Hello World!";Intent notificationIntent = new Intent(this, MyClass.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
  12. 12. Status Bar Notification : code snippetPass the Notification to the NotificationManager:private static final int HELLO_ID = 1;mNotificationManager.notify(HELLO_ID, notification); – Thats it. Your user has now been notified.Reference : http://developer.android.com/guide/topics/ui/notifiers/notifications.html
  13. 13. Dialog Notification A dialog is usually a small window that appears in front of the current Activity. The underlying Activity loses focus and the dialog accepts all user interaction. Dialogs are normally used for notifications and short activities that directly relate to the application in progress.
  14. 14. Dialog Notification: Types The Dialog class is the base class for creating dialogs. However, you typically should not instantiate a Dialog directly. Instead, you should use one of the following subclasses:  AlertDialog  ProgressDialog  DatePickerDialog  TimePickerDialog
  15. 15. Alert Dialog A dialog that can manage zero, one, two, or three buttons, and/or a list of selectable items that can include checkboxes or radio buttons.AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MyActivity.this.finish(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } });AlertDialog alert = builder.create();
  16. 16. Alert Dialogfinal CharSequence[] items = {"Red", "Green", "Blue"};AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("Pick a color");builder.setSingleChoiceItems(items, -1, newDialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Toast.makeText(getApplicationContext(), items[item],Toast.LENGTH_SHORT).show(); }});AlertDialog alert = builder.create();
  17. 17. ProgressDialog A ProgressDialog is an extension of the AlertDialog class that can display a progress animation in the form of a spinning wheel, for a task with progress thats undefined, or a progress bar, for a task that has a defined progression.ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", "Loading. Please wait...", true);
  18. 18. Custom DialogPlease Refer : http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
  19. 19. DatePickerDialog To provide a widget for selecting a date, use the DatePicker widget, which allows the user to select the month, day, and year, in a familiar interface.@Overrideprotected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); } return null;}
  20. 20. DatePickerDialog//the callback received when the user "sets" the date in the dialog– private DatePickerDialog.OnDateSetList ener mDateSetListener =– new DatePickerDialog.OnDateSetList ener() {– public void
  21. 21. Time Picker DialogPlease Refer:http://developer.android.com/resources/tutorials/views/hello-timepicker.html
  22. 22. End Of Notifications
  23. 23. Debugging Android Application
  24. 24. Debugging Android Application The Android SDK provides most of the tools that you need to debug your applications. You need a JDWP-compliant debugger if you want to be able to do things such as step through code, view variable values, and pause execution of an application. If you are using Eclipse, a JDWP-compliant debugger is already included and there is no setup required.
  25. 25. Debug Tools Adb Dalvik Debug Monitor Server Device or Android Virtual Device Heirarchy Viewer and layoutopt Traceview Dev Tools Android application
  26. 26. ADB adb acts as a middleman between a device and your development system. It provides various device management capabilities, including moving and syncing files to the emulator, running a UNIX shell on the device or emulator, and providing a general means to communicate with connected emulators and devices.
  27. 27. Dalvik Debug Monitor Server DDMS is a graphical program that communicates with your devices through adb. DDMS can capture screenshots, gather thread and stack information, spoof incoming calls and SMS messages, and has many other features.
  28. 28. Device or Android Virtual Device Your application must run in a device or in an AVD so that it can be debugged. An adb device daemon runs on the device or emulator and provides a means for the adb host daemon to communicate with the device or emulator.
  29. 29. Heirarchy Viewer and layoutopt Graphical programs that let you debug and profile user interfaces.
  30. 30. Traceview A graphical viewer that displays trace file data for method calls and times saved by your application, which can help you profile the performance of your application.
  31. 31. End of Debugging android applicationReference: http://developer.android.com/guide/developing/debugging/index.html
  32. 32. Intent and intent filters
  33. 33. Intent and Intent filters Three of the core components of an application — activities, services, and broadcast receivers — are activated through messages, called intents. Intent messaging is a facility for late run-time binding between components in the same or different applications. The intent itself, an Intent object, is a passive data structure holding an abstract description of an operation to be performed — or, often in the case of broadcasts, a description of something that has happened and is being announced.
  34. 34. Intent and Intent filters An Intent object is a bundle of information. It contains information of interest to the component that receives the intent (such as the action to be taken and the data to act on) plus information of interest to the Android system (such as the category of component that should handle the intent and instructions on how to launch a target activity).  Component Name  Action  Data  Category  Extras  Flags
  35. 35. Component Name The name of the component that should handle the intent. This field is a ComponentName object — a combination of the fully qualified class name of the target component (for example "com.example.project.app.FreneticActivity") and the package name set in the manifest file of the application where the component resides (for example, "com.example.project"). The package part of the component name and the package name set in the manifest do not necessarily have to match.Note : The component name is optional. If it is set, the Intent object is delivered to an instance of the designated class. If it is not set, Android uses other information in the Intent object to locate a suitable target
  36. 36. ActionA string naming the action to be performed — or, in the case ofbroadcast intents, the action that took place and is being reported. TheIntent class defines a number of action constants, including these: The action in an Intent object is set by the setAction() method and read by getAction().
  37. 37. Data The URI of the data to be acted on and the MIME type of that data. Different actions are paired with different kinds of data specifications. For example, if the action field is ACTION_EDIT, the data field would contain the URI of the document to be displayed for editing. If the action is ACTION_CALL, the data field would be a tel: URI with the number to call. Similarly, if the action is ACTION_VIEW and the data field is an http: URI, the receiving activity would be called upon to download and display whatever data the URI refers to. The setData() method specifies data only as a URI, setType() specifies it only as a MIME type, and setDataAndType() specifies it as both a URI and a MIME type. The URI is read by getData() and the type by getType().
  38. 38. Category A string containing additional information about the kind of component that should handle the intent. Any number of category descriptions can be placed in an Intent object. The addCategory() method places a category in an Intent object, removeCategory() deletes a category previously added, and getCategories() gets the set of all categories currently in the object. Example : CATEGORY_BROWSABLE, CATEGORY_GADGET , CATEGORY_HOME , CATEGORY_LAUNCHER , CATEGORY_PREFERENCE
  39. 39. Extras Key-value pairs for additional information that should be delivered to the component handling the intent. The Intent object has a series of put...() methods for inserting various types of extra data and a similar set of get...() methods for reading the data. These methods parallel those for Bundle objects. In fact, the extras can be installed and read as a Bundle using the putExtras() and getExtras() methods.
  40. 40. Flags Flags of various sorts. Many instruct the Android system how to launch an activity (for example, which task the activity should belong to) and how to treat it after its launched (for example, whether it belongs in the list of recent activities). All these flags are defined in the Intent class.References :http://developer.android.com/guide/appendix/g-app-intents.htmlhttp://developer.android.com/guide/topics/intents/intents-filters.html
  41. 41. Intent ResolutionIntents can be divided into two groups: Explicit intents designate the target component by its name (the component name field, mentioned earlier, has a value set). Since component names would generally not be known to developers of other applications, explicit intents are typically used for application- internal messages — such as an activity starting a subordinate service or launching a sister activity. Implicit intents do not name a target (the field for the component name is blank). Implicit intents are often used to activate components in other applications.
  42. 42. Intent Filter Code snippetAction Test:<intent-filter . . . > <action android:name="com.example.project.SHOW_CURRENT" /> <action android:name="com.example.project.SHOW_RECENT" /> <action android:name="com.example.project.SHOW_PENDING" /> ...</intent-filter>
  43. 43. Intent Filter Code snippetCategory test :<intent-filter . . . > <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> ...</intent-filter>
  44. 44. Intent Filter Code snippetData test :<intent-filter . . . > <data android:mimeType="video/mpeg" android:scheme="http" . . . /> <data android:mimeType="audio/mpeg" android:scheme="http" . . . /> ...</intent-filter>
  45. 45. Intent Filter Code snippetCommon cases :<data android:mimeType="image/*" /><data android:scheme="http" android:type="video/*" /><intent-filter . . . > <action android:name="code android.intent.action.MAIN" /> <category android:name="code android.intent.category.LAUNCHER" /></intent-filter>
  46. 46. End of Intent and Intent Filters
  47. 47. 2. Multimedia in android
  48. 48. Multimedia in android The Android multimedia framework includes support for capturing and playing audio, video and images in a variety of common media types, so that you can easily integrate them into your applications. You can play audio or video from media files stored in your applications resources, from standalone files in the file system, or from a data stream arriving over a network connection, all using the MediaPlayer or JetPlayer APIs. You can also record audio, video and take pictures using the MediaRecorder and Camera APIs if supported by the device hardware.Reference : http://developer.android.com/guide/topics/media/index.html
  49. 49. Multimedia in android Please refer Following link to see the Media formats supported by android:http://developer.android.com/guide/appendix/media-formats.html
  50. 50. Multimedia supported Audio formatsAudio Formats :  AAC LC/LTP  HE-AACv1 (AAC+)  HE-AACv2 (enhanced AAC+)  AMR-NB (.3GP)  AMR-WB (.3GP)  FLAC (Android 3.1 +)  MP3 (.mp3)  MIDI  Vorbis (.ogg ) (.mkv android 4.0+)  PCM/WAVE (.wav)
  51. 51. Simple Media PlayBackUsing Media Player: One of the most important components of the media framework is the MediaPlayer class. An object of this class can fetch, decode, and play both audio and video with minimal setup. It supports several different media sources such as: 1. Local resources 2. Internal URIs, such as one you might obtain from a Content Resolver 3. External URLs (streaming)
  52. 52. Media Player code snippetFrom Local file in raw folder : res/rawMediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);mediaPlayer.start();// no need to call prepare(); create() does that for you
  53. 53. Media Player code snippetPlay from a URI available locally in the system (that you obtained through a Content Resolver, for instance):Uri myUri = ....; // initialize Uri hereMediaPlayer mediaPlayer = new MediaPlayer();mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);mediaPlayer.setDataSource(getApplicationContext(), myUri);mediaPlayer.prepare();mediaPlayer.start();
  54. 54. Media Player code snippetPlay From URL external site :String url = "http://........"; // your URL hereMediaPlayer mediaPlayer = new MediaPlayer();mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);mediaPlayer.setDataSource(url);mediaPlayer.prepare(); // might take long! (for buffering,etc)mediaPlayer.start();
  55. 55. Media Player More Info Please refer :http://developer.android.com/guide/topics/media/mediaplayer.html
  56. 56. Supported video formats Please Refer This Link for supported vedio formats http://developer.android.com/guide/appendix/media-formats.html
  57. 57. 3.Sql database Introducing sqlite Sqlite open helper and creating a database Opening and closing a database4.Basic content providers Content provider mime types Searching for content Adding, changing, and removing content
  58. 58. SQLite Database
  59. 59. Introducing Sqlite Database Store structured data in a private database Android provides full support for SQLite databases. Any databases you create will be accessible by name to any class in the application, but not outside the application. The recommended method to create a new SQLite database is to create a subclass of SQLiteOpenHelper and override the onCreate() method, in which you can execute a SQLite command to create tables in the database.
  60. 60. Introducing Sqlite Databasepublic class DictionaryOpenHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 2; private static final String DICTIONARY_TABLE_NAME = "dictionary"; private static final String DICTIONARY_TABLE_CREATE = "CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" + KEY_WORD + " TEXT, " + KEY_DEFINITION + " TEXT);"; DictionaryOpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DICTIONARY_TABLE_CREATE); }}
  61. 61. Introducing Sqlite Database You can then get an instance of your SQLiteOpenHelper implementation using the constructor youve defined. To write to and read from the database, call getWritableDatabase() and getReadableDatabase(), respectively. These both return a SQLiteDatabase object that represents the database and provides methods for SQLite operations.
  62. 62. Introducing Sqlite Database You can execute SQLite queries using the SQLiteDatabase query() methods, which accept various query parameters, such as the table to query, the projection, selection, columns, grouping, and others. For complex queries, such as those that require column aliases, you should use SQLiteQueryBuilder, which provides several convienent methods for building queries. Every SQLite query will return a Cursor that points to all the rows found by the query. The Cursor is always the mechanism with which you can navigate results from a database query and read rows and columns.
  63. 63. Introducing Sqlite DatabaseNote : Android does not impose any limitations beyond the standard SQLite concepts. It is recommend including an autoincrement value key field that can be used as a unique ID to quickly find a record. This is not required for private data, but if you implement a content provider, you must include a unique ID using the BaseColumns._ID constant.
  64. 64. Practical Approach and Good Practices Reference :http://www.vogella.de/articles/AndroidSQLite/article.html#overview_sqlite
  65. 65. Basic content providers
  66. 66. 5.Graphics and Animations Drawing graphics in android Drawing with xml Canvas drawing best practice6.Accessing android hardware Using the media apis Using the camera Using the accelerometer and compasshttp://www.devx.com/wireless/article/42482/1954http://www.devx.com/wireless/Article/43005/0/page/2

×