Android activity, service, and broadcast recievers


Published on

Published in: Technology

Android activity, service, and broadcast recievers

  1. 1.
  2. 2. Hello Android <ul><li>Create a new project by – file -> New Project -> Android Project. </li></ul><ul><li>Enter the details - </li></ul><ul><li>Project Name – HelloWorld </li></ul><ul><li>Build Target – Android 2.2 (API 8) </li></ul><ul><li>Application Name – Hello Android </li></ul><ul><li>Package name – </li></ul><ul><li>Activity name – HelloWorldActivity </li></ul><ul><li>Click o finish, and wait for the project to be created. </li></ul><ul><li>The build your project. </li></ul><ul><li>Then click o the play icon In the taskbar on the top, or right click on the project folder In the workspace, and then select – android application </li></ul><ul><li>Select the AVD corresponding to the build target, and then wait for the emulator to load </li></ul>
  3. 3. New Project Select AVD
  4. 4. Understanding Hello world Android Project file structure <ul><li>Project Name </li></ul><ul><li>Src </li></ul><ul><li>Gen </li></ul><ul><li>Android version Libraries </li></ul><ul><li>Assets </li></ul><ul><li>Res </li></ul><ul><li>- drawable-hdpi </li></ul><ul><li>-drawable-ldpi </li></ul><ul><li>-drawable-mdpi </li></ul><ul><li>-layout </li></ul><ul><li>-values </li></ul><ul><li>AndroidManifest.xml </li></ul><ul><li> </li></ul>
  5. 5. Brief Description about the Folders Src an assets folder This src folder, contains the actual source code Res and assets folder The Resource aka res folder, contains all the resource files which are divided into generally 5 folders a. drawable – hdpi b drawable – ldpi c. drawable – mdpi d. layout e. values Apart from all other files are added to the assets folder like java projects,
  6. 6. The gen folder The gen folder contains the file This file is auto generated R.Java contains set of unique precompiled IDs assigned to every resource in the res folder. The file This file generally contains the information regarding the version of Android used to make the application. But, in order to specify or change the build environment or parameters we’ll modify this file
  7. 7. AndroidManifest. xml The manifest lets you define the structure and metadata of your application, its components, and its requirements. Breaking down 1. Activity tag – To describe any new activity in the application 2 . Application , intent-filter tag <application android:icon=&quot;@drawable/icon&quot; android:label=&quot;@string/app_name&quot;> <activity android:name=&quot;.DatePickerActivity&quot; android:label=&quot;@string/app_name&quot;> <intent-filter> <action android:name=&quot;android.intent.action.MAIN&quot; /> <category android:name=&quot;android.intent.category.LAUNCHER&quot; /> </intent-filter> </activity> </application>
  8. 8. 3. Android Permissions (uses-application tag) The Android permissions are the set of hardware/software permissions to be taken by the current application. <uses-permission android:name= &quot;android.permission.INTERNET&quot; />
  9. 9. Android Activity An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. The very first step in developing an Android application is making a Activity Your application must have at least one Activity.
  10. 10. Importance of Activity An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI . While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows or embedded inside of another activity (using ActivityGroup).
  11. 11. Creating an Activity To create an activity, you must create a subclass of Activity (or an existing subclass of it). In your subclass, you need to implement the most important callback methods are: onCreate() The system calls this when creating your activity. Most importantly, this is where you must call setContentView() to define the layout for the activity's user interface. onPause() This is usually where we should commit any changes that should be persisted beyond the current user session (because the user might not come back).
  12. 12. Dissection of an Activity <ul><li>Some of the important overridden methods in the Activity Class </li></ul><ul><li>onStart(); </li></ul><ul><li>onRestart(); </li></ul><ul><li>onResume(); </li></ul><ul><li>onPause(); </li></ul><ul><li>onStop(); </li></ul><ul><li>onDestroy(); </li></ul><ul><li>void finish(); </li></ul><ul><li>void finishActivity(int requestCode); </li></ul><ul><li>setContentView(int layoutResID); </li></ul><ul><li>void setTitle(CharSequence title) </li></ul><ul><li>startActivity(Intent intent) </li></ul><ul><li>setIntent(Intent newIntent) </li></ul><ul><li>View findViewById(int id) ; </li></ul><ul><li>PendingIntent createPendingResult(int requestCode, Intent data, int flags); </li></ul><ul><li>Boolean onKeyEvent(KeyEvent event); </li></ul><ul><li>boolean onTouchEvent(MotionEvent ev); </li></ul><ul><li>boolean onTrackballEvent(MotionEvent ev); </li></ul>
  13. 13. <ul><li>LayoutInflater getLayoutInflater() </li></ul><ul><li>WindowManager getWindowManager() </li></ul><ul><li>Window getWindow() </li></ul><ul><li>Dialog onCreateDialog(int id) </li></ul><ul><li>boolean onCreatePanelMenu(int featureId, Menu menu) </li></ul><ul><li>View onCreatePanelView(int featureId) </li></ul><ul><li>Boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) </li></ul><ul><li>View onCreateView(String name, Context context, AttributeSet attrs) </li></ul><ul><li>boolean onMenuItemSelected(int featureId, MenuItem item) </li></ul><ul><li>startSearch(String initialQuery, boolean selectInitialQuery,Bundle appSearchData, boolean globalSearch) </li></ul><ul><li>startIntentSender(IntentSender intent, Intent fillInIntent,int flagsMask, int flagsValues, int extraFlags) </li></ul><ul><li>boolean startActivityIfNeeded(Intent intent, int requestCode) </li></ul>
  14. 14. Activity Life Cycle
  15. 15. Summary of Activity Life cycle
  16. 16. Android Service A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC).  
  17. 17. Broadcast Receivers A broadcast receiver is a class which extends &quot;BroadcastReceiver&quot; and which is registered as a receiver in an Android Application via the AndroidManifest.xml (or via code). This class will be able to receive intents via the sendBroadcast() method. Broadcast receiver is a component that responds to system-wide broadcast announcements
  18. 18. References <ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>