Android Application Developement Short Notes

1). Android is an open source and Linux-based Operating System for mobile devices such as smartphones and tablet computers. Android was developed by the Open Handset Alliance, led by Google, and other companies.

2). Android applications are usually developed in the Java language using the Android Software Development Kit.

3). Once developed, Android applications can be packaged easily and sold out either through a store such as Google Play or the Amazon Appstore.

4). Window > AVD Manager> which will launch Android AVD Manager. Use New button to create a new Android Virtual Device and enter the following information, before clicking Create AVD button.

5). Android operating system is a stack of software components which is roughly divided into five sections :
Application >> Application Framework >> Libraries >> Android Runtime >> Linux Kernal

6). At the bottom of the layers is Linux - Linux 2.6 with approximately 115 patches.This provides basic system functionality like process management, memory management, device management like camera, keypad, display etc. Also, the kernel handles all the things that Linux is really good at such as networking and a vast array of device drivers, which take the pain out of interfacing to peripheral hardware.

7). Android Runtime provides a key component called Dalvik Virtual Machine which is a kind of Java Virtual Machine specially designed and optimized for Android.

8). Application components are the essential building blocks of an Android application. These components are loosely coupled by the application manifest file AndroidManifest.xml that describes each component of the application and how they interact.

9). There are following four main components that can be used within an Android application:
Activities : They dictate the UI and handle the user interaction to the smartphone screen
Services :They handle background processing associated with an application.
Broadcast Receivers :They handle communication between Android OS and applications.
Content Providers   :They handle data and database management issues.

10). Activities : An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. If an application has more than one activity, then one of them should be marked as the activity that is presented when the application is launched.
An activity is implemented as a subclass of Activity class as follows:
Code:
------
public class MainActivity extends Activity {

}

11). Services : A service is a component that runs in the background to perform long-running operations. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity.

A service is implemented as a subclass of Service class as follows:
Code:
------
public class MyService extends Service {

}

12). Broadcast Receivers : Broadcast Receivers simply respond to broadcast messages from other applications or from the system. For
example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and each message is broadcasted as an Intent object.
Code:
-----
public class MyReceiver extends BroadcastReceiver {

}

13). Content Providers : A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. The data may be stored in the file system, the database or somewhere else entirely.

A content provider is implemented as a subclass of ContentProvider class and must implement a standard set of APIs that enable other applications to perform transactions.

Code:
------
public class MyContentProvider extends ContentProvider {

}

14). Additional Components :
Fragments: Represents a behavior or a portion of user interface in an Activity.
Views : UI elements that are drawn onscreen including buttons, lists forms etc.
Layouts : View hierarchies that control screen format and appearance of the views.
Intents : Messages wiring components together.
Resources : External elements, such as strings, constants and drawables pictures.
Manifest : Configuration file for the application.

15). Folder, File & Description :
src : This contains the .java source files for your project. By default, it includes an MainActivity.java source file 
  having an activity class that runs when your app is launched using the app icon.

gen: This contains the .R file, a compiler-generated file that references all the resources found in your project. You 
  should not modify this file.

bin: This folder contains the Android package files .apk built by the ADT during the build process and everything else 
  needed to run an Android application.

res/drawable-hdpi: This is a directory for drawable objects that are designed for high-density screens.

res/layout : This is a directory for files that define your app's user interface.

res/values : This is a directory for other various XML files that contain a collection of resources, such as strings and colors
     definitions.

AndroidManifest.xml : This is the manifest file which describes the fundamental characteristics of the app and defines each of 
      its components.


16). The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application.

17). The onCreate() method is one of many methods that are fired when an activity is loaded.

18). Whatever component you develop as a part of your application, you must declare all its components in a manifest file called AndroidManifest.xml which ressides at the root of the application project directory. This file works as an interface between Android OS and your application, so if you do not declare your component in this file, then it will not be considered by the OS.

18.1). Here <application>...</application> tags enclosed the components related to the application. Attributeandroid:icon will point to the application icon available under res/drawable-hdpi. The application uses the image named 
ic_launcher.png located in the drawable folders

18.2).  The <activity> tag is used to specify an activity and android:name attribute specifies the fully qualified class name
of the Activity subclass and the android:label attributes specifies a string to use as the label for the activity. You 
can specify multiple activities using <activity> tags.

18.3). The action for the intent filter is named android.intent.action.MAIN to indicate that this activity serves as the entry
point for the application. The category for the intent-filter is namedandroid.intent.category.LAUNCHER to indicate
that the application can be launched from the device's launcher icon.

19). Following is the list of tags which you will use in your manifest file to specify different Android application components:
a). <activity> elements for activities
b). <service> elements for services
c). <receiver> elements for broadcast receivers
d). <provider> elements for content providers

20). The strings.xml file is located in the res/values folder and it contains all the text that your application uses. For example, the names of buttons, labels, default text, and similar types of strings go into this file. This file is responsible for their textual content. For example, a default strings file will look like as following file:

Code for the String.xml :
-------------------------
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>

21). The gen/com.example.helloworld/R.java file is the glue between the activity Java files likeMainActivity.java and the resources like strings.xml. It is an automatically generated file and you should not modify the content of the R.java file.

22). The activity_main.xml is a layout file available in res/layout directory, that is referenced by your application when building its interface. You will modify this file very frequently to change the layout of your application. 

23). Directories of res folder:
anim/:XML files that define property animations. They are saved in res/anim/ folder and accessed from the R.anim 
class.

color/ :XML files that define a state list of colors. They are saved in res/color/ and accessed from the R.color class.

drawable/ : Image files like .png, .jpg, .gif or XML files that are compiled into bitmaps, state lists, shapes, animation drawables. They are saved in res/drawable/ and accessed from the R.drawable class.

layout/ :XML files that define a user interface layout. They are saved in res/layout/ and accessed from the R.layout 
class.

menu/ :XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. They are saved in res/menu/ and accessed from theR.menu class.

raw/ :Arbitrary files to save in their raw form. You need to callResources.openRawResource() with the resource ID, which is R.raw.filename to open such raw files.

values/ :XML files that contain simple values, such as strings, integers, and colors. For example, here are some 
filename conventions for resources you can create in this directory:
a). arrays.xml for resource arrays, and accessed from the R.array class.
b). integers.xml for resource integers, and accessed from the R.integerclass.
c). bools.xml for resource boolean, and accessed from the R.bool class.
d). colors.xml for color values, and accessed from the R.color class.
e). dimens.xml for dimension values, and accessed from the R.dimen class.
f). strings.xml for string values, and accessed from the R.string class.
g). styles.xml for styles, and accessed from the R.style class


24). xml/ : Arbitrary XML files that can be read at runtime by calling Resources.getXML(). You can save various configuration files here which will be used at run time.

25). Alternative Resources : Your application should provide alternative resources to support specific device configurations. For example, you should include alternative drawable resources ( ie.images ) for different screen resolution and alternative string resources for different languages. At runtime, Android detects the current device configuration and loads the appropriate resources for your application.

For example :
-------------
drawable/icon.png, background.png
drawable-hdpi/icon.png, background.png
drawable-ar/icon.png, background.png

26). ACCESSING RESOURCES IN CODE : When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.
EXAMPLE: (To access res/drawable/myimage.png and set an ImageView you will use following code:)
---------
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);

27). Example :
     ---------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello, World!</string>
</resources>

From Code:
------------
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello);

28). Example :
     ----------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<string name="hello">Hello!</string>
</resources>

From Code:
-----------
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/opaque_red"
android:text="@string/hello" />

29). An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. If an application has more than one activity, then one of them should be marked as the activity that is presented when the application is launched.

30). Android system initiates its program with in anActivity starting with a call on onCreate() callback method. 

31). Example of activity callback funtions :
onResume(), onPause(), onStop(), onStart(), onCreate(), onDestroy(), onRestart()

32). The Activity class defines the following callbacks i.e. events.
onCreate() : This is the first callback and called when the activity is first created.
onStart() : This callback is called when the activity becomes visible to the user.
onResume() : This is called when the user starts interacting with the application.
onPause() : The paused activity does not receive user input and cannot execute any code and called when the current 
  activity is being paused and the previous activity is being resumed.
onStop() : This callback is called when the activity is no longer visible.
onDestroy() : This callback is called before the activity is destroyed by the system.
onRestart() : This callback is called when the activity restarts after stopping it.

33). An application can have one or more activities without any restrictions. Every activity you define for your application must be declared in your AndroidManifest.xml file and the main activity for your app must be declared in the manifest with an <intent-filter> that includes the MAIN action and LAUNCHER category.
Code:
-----
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

34). A service is a component that runs in the background to perform long-running operations without needing to interact with the user. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. 

35). Types of services :
a). Started : A service is started when an application component, such as an activity, starts it by calling startService().     Once started, a service can run in the background indefinitely, even if the component that started it is destroyed.

b). Bound : A service is bound when an application component binds to it by callingbindService(). A bound service offers a     client-server interface that allows components to interact with the service, send requests, get results, and even do so     across processes with interprocess communication (IPC).

36). Callbacks of Service Class :
a). onStartCommand()  : The system calls this method when another component, such as an activity, requests that the service be started, by calling startService(). If you implement this method, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService() methods.

37). Content Providers : A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network.

38). Content providers let you centralize content in one place and have many different applications access it as needed. A content provider behaves very much like a database where you can query it, edit its content, as well as add or delete content usingg insert(), update(), delete(), and query() methods. In most cases this data is stored in an SQlite database.

39). Content URIs : To query a content provider, you specify the query string in the form of a URI which has following format:
<prefix>://<authority>/<data_type>/<id>
Here is the detaial of various parts of the URI:
------------------------------------------------------
prefix:  This is always set to content://

authority:  This specifies the name of the content provider, for example contacts, browser etc. For third-party content    providers, this could be the fully qualified name, such ascom.tutorialspoint.statusprovider.

data_type:  This indicates the type of data that this particular provider provides. For example, if you are getting all    the contacts from the Contacts content provider, then the data path would be people and URI would look like    this content://contacts/people

Id: This specifies the specific record requested. For example, if you are looking for contact number 5 in the 
  Contacts content provider then URI would look like thiscontent://contacts/people/5.

40). Create Content Provider : This involves number of simple steps to create your own content provider.
a). First of all you need to create a Content Provider class that extends the ContentProviderbaseclass.

b). Second, you need to define your content provider URI address which will be used to access the content.

c). Next you will need to create your own database to keep the content. Usually, Android uses SQLite database and framework     needs to override onCreate() method which will use SQLite Open Helper method to create or open the provider's databse.     When your application is launched, theonCreate() handler of each of its Content Providers is called on the main     application thread.

d). Next you will have to implement Content Provider queries to perform different database specific operations.

e). Finally register your Content Provider in your acitivity file using <provider> tag.

41). Here is the list of methods which you need to override in Content Provider class to have your Content Provider working:
a). onCreate() : This method is called when the provider is started.
b). query() : This method receives a request from a client. The result is returned as a Cursor object.
c). insert(): This method inserts a new record into the content provider.
d). delete() : This method deletes an existing record from the content provider.
e). update(): This method updates an existing record from the content provider.
f). getType()   : This method returns the MIME type of the data at the given URI.