Android Architecture



Android operating system is stack of software components which is divided in four layers and five sections.



Linux kernel : 
Linux kernel provides a level of abstraction between the device hardware and it contains all the essential hardware drivers 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.

Libraries : 
On top of Linux kernel there is a set of libraries including open-source Web browser engine WebKit, well known library libc, SQLite database which is a useful repository for storage and sharing of application data, libraries to play and record audio and video, SSL libraries responsible for Internet security etc.

Android Libraries : 
This category encompasses Java-based libraries that are specific to Android development. Libraries in this category include the application framework libraries in addition to those that facilitate user interface building, graphics drawing and database access.

  • android.app − Provides access to the application model.
  • android.content − Facilitates content access, publishing and messaging between applications and application components. 
  • android.database − Used to access data published by content providers and includes SQLite database management classes.
  • android.opengl − A Java interface to the OpenGL ES 3D graphics rendering API.
  • android.os − Provides applications with access to standard operating system services including messages, system services and inter-process communication.
  • android.text − Used to render and manipulate text on a device display.
  • android.view − The fundamental building blocks of application user interfaces.
  • android.widget − A rich collection of pre-built user interface components such as buttons, labels, list views, layout managers, radio buttons etc.
  • android.webkit − A set of classes intended to allow web-browsing capabilities to be built into applications.

Android Runtime : 

This section provides a key component called Dalvik Virtual Machine which is a kind of Java Virtual Machine specially designed and optimized for Android.
The Dalvik VM makes use of Linux core features like memory management and multi-threading, which is intrinsic in the Java language. The Dalvik VM enables every Android application to run in its own process, with its own instance of the Dalvik virtual machine.
The Android runtime also provides a set of core libraries which enable Android application developers to write Android applications using standard Java programming language.
Application Framework : 
The Application Framework layer provides many higher-level services to applications in the form of Java classes. Application developers are allowed to make use of these services in their applications.
          It provides the following services.
Activity Manager − Controls all aspects of the application lifecycle and activity stack.
  • Content Providers − Allows applications to publish and share data with other applications.
  • Resource Manager − Provides access to non-code embedded resources such as strings, color settings and user interface layouts.
  • Notifications Manager − Allows applications to display alerts and notifications to the user.
  • View System − An extensible set of views used to create application user interfaces.

Applications : 
Android application at the top layer. You will write your application to be installed on this layer only. Examples of such applications are Contacts Books, Browser, Games etc.


instanceof operator



instanceof operator is also known as type comparison operator. It compares an object with any specific type(may be class, interface etc.). It returns either true or false.
It is used to test whether given object is an instance of specified class or not.

Example :

class Test{
public static void main(String args[]){
Test t=new Test();
System.out.println(t instanceof Test ) ;

}

Output :
true

Ternary operator



In the last post we learned some basic operators. Apart from these operators, there are some other operators we need to know about.

In this post post, we will learn Ternary operator.


Ternary operator(?:) : 
                                   This operator is also called conditional operator. It   contains three operands and is used to evaluate boolean expressions. Goal of this operator is to decide which value should be assigned to variable.
Syntax :
 Variable v=(expression) ? value if true : value if false ;                                                                               
Example : 

  public class TernaryOperatorTest {
  public static void main(String args[]) {
      int x, y;
      x = 5;
      y = (x == 1) ? 10: 15;
      System.out.println( "Value of y : " +  y );
      y = (x == 5) ? 10: 15;
      System.out.println( "Value of y : " + y );
   }
}


Output : 


Value of y : 15
Value of y : 10

Previous topic                                                                  Next topic
Java operators                                                                 instanceof operator







Operators in Java




Operator is a symbol to perform a certain operation on given operands.

Type of operators

1 . Arithmetic operators

2 . Relational operators

3 . Bitwise operators

4 . Logical operators

5 . Assignment operators


Now we will go through each kind of operator one by one


Arithmetic operator : 

These operators are used in mathematical expressions in the same way that they are used in algebra.
Assume integer variables X and Y having values 10 and 5 respectively.

   + (Addition) : Adds values on either side of operator.  
                                X+Y=10+5 gives 15 as result.

    - (Subtraction) : Subtracts second value from the first one.
                              X-Y =10-5 gives 5 as result.

   * (Multiplication) :  Multiplies values given on either side.
                                X*Y=10*5 gives 50 as result.

   / (Division) : Divides first value by second one.
                       X/Y=10/5 gives 2 as result.  

  % (modulus) : Gives remainder if, a value is divided by another.
                          X%Y=10%5 gives 0 as result.

  ++ (Increment operator) : Increases a value by 1.
                                        X++ is equivalent to x+1 i.e. 10+1 gives 11 as 
                                         result.

  -- (Decrement operator) : Decreases a value by 1.
                                         X-- is equivalent to X-1 i.e. 10-1 gives 9 as result. 

Relational Operators : 

 Relational operators are used to compare operands.

Assume two integers X and Y having 10 and 5 respectively.

There are following type of relational operator.

==(equal to) :  Checks if the values of two operands are equal or not, if yes  
                          then condition becomes true.
                         (X==Y) is false.

!= (not equal to) : Checks if the values of two operands are equal or not, if 
                              values are not equal then condition becomes true.
                             (X!=Y) is true.

> (greater than) : Checks if the value of left operand is greater than the                                      value of right operand, if yes then condition becomes                                    true.
                           (X>Y) is true.

< (less than) :  Checks if the value of left operand is smaller than the value 
                        of  right operand, if yes then condition becomes true.
                        (X<Y) is false.

>= (greater than or equal to) :  Checks if the value of left operand is 
                                                  greater or equal to value of the left operand.
                                                  (X>=Y) is true.

<= (less than or equal to) :   Checks if the value of left operand is smaller
                                                   or equal to value of the left operand.
                                                  (X<=Y) is false.  

Bitwise operator :

Java Bitwise operators can be applied to integer types, long, int, short, char and byte.
Bit wise operator works on bits.
Assume int X=10 and Y=5 and their bit conversions are as follows
X=00001010
Y=00000101

 & (Bitwise AND) : Binary AND operator copies a bit to the result if it exists  
                              in both operands.
                             X&Y=00000000.

| ( bitwise OR) :    Binary OR operator copies a bit if it exists in either 
                               operand.
                               X | Y = 00001111.

^ (Bitwise XOR) : Binary XOR operator copies bit if it is set in one operand                                    but not both.
                              X ^ Y = 00001111.

~ (Bitwise complement) : It is used to determine the negation of given 
                                       value.It is 2's complement of the given value
                                       ~X = 11110110

<< (Left shift operator) : The left operand value is moved left by the  
                                       number of bits specified by right operand.
                                       X<<2 = 10<<2 = 00101000

>> ( Right shift operator) : The left operand value is moved right by 
                                             the number of bits specified by right operand.  
                                            X>>2 = 10>>2 = 10000010


Logical Operator :

This operator returns result in boolean(true or false).
Assume there are two boolean variables X and Y having values true and false respectively.

&& (Logical AND) : If both operands are true then (X&&Y) becomes true 
                             otherwise becomes false.

|| (Logical OR) : If any one of the operands has value true (X||Y) becomes 
                          true.

! (Logical NOT) : Reverse the logical state of the operand (!X) is false (!Y) is 
                          true 

Assignment Operator : 
           
This operator is used to assign a value to a variable.
e.g. int x=5; 


Previous topic                                                                     Next topic
Unicode system                                                                  Ternary operator

            
                                     



     

Unicode System



Unicode system  is universal international standard character encoding which is capable to represent most of the world's languages.

Before Unicode system there were several encoding systems


1 . ASCII - Supports language of united states.

2 . ISO 8859-1 - It supports western European language.
3 . KOI-8 - Supports Russian language.
4 . GB18030 and BIG-5 - Supports Chinese language.

This caused the following problem.


A particular code value corresponds to different letters in the various language standards and The encodings for languages with large character sets have variable length.Some common characters are encoded as single bytes, other require two or more byte.


                 To solve this problem, A new encoding system was developed called Unicode system which supports world's most of the languages. In unicode, character holds 2 bytes, so java also uses 2 bytes for characters.


Lowest value in unicode system- \u0000

Highest value in unicode system- \uFFFF

Previous topic                                                                                                   Next topic
Jdk, jre, jvm                                                                                                     Java operators

SQLite database in android

SQLite is an open-source relational database i.e. used to perform database operations on devices such as storing, manipulating or retrieving data from the database.
It is embedded in android by default. So, there is no need to perform any database setup .
Here, we are going to see the example of sqlite to store, fetch, update and delete the data.
SQLiteOpenHelper class provides the functionality to use the SQLite database. It is available in android.database.sqlite package, is used for database creation and version management. For performing any database operation, you have to provide the implementation of onCreate() and onUpgrade() methods.
To create a table
@Override
 public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("create table\t"+TABLE_NAME+"\n"+"("+
            COLUMN_ONE+"\t integer primary key ,\n"+
            COLUMN_TWO+"\t"+"varchar(50),\n"+
            COLUMN_THREE+"\t"+"varchar(20));");
 }
Inset a record in created table
  public void insertRecord(Customer customer){
    SQLiteDatabase db=getWritableDatabase();
    ContentValues cv=new ContentValues();
    cv.put(COLUMN_ONE,customer.getCustomerId());
    cv.put(COLUMN_TWO,customer.getCustomerName());
    cv.put(COLUMN_THREE,customer.getCustomerNumber());
    db.insert(TABLE_NAME,null,cv);
 }
Delete a row from the table
public boolean delete(String customerId){

    boolean status=false;
    SQLiteDatabase db=getWritableDatabase();
    if(db!=null){

        status=db.delete(TABLE_NAME,COLUMN_ONE+"\t="+customerId,null)>0;
    }
    return status;
}
Update a record in a table
   public boolean update(String customerId,String customerName,String customerNumber){
    boolean status=false;
    String updateQuery="Update table\t"+TABLE_NAME+"\t set\t"+COLUMN_TWO+"\t="+customerName+"\t,"+
                        COLUMN_THREE+"\t="+customerNumber+"\t where\t"+COLUMN_ONE+"="+customerId;
    SQLiteDatabase db=getWritableDatabase();
    if(db!=null){
        ContentValues cv=new ContentValues();
        cv.put(COLUMN_TWO,customerName);
        cv.put(COLUMN_THREE,customerNumber);
       status=db.update(TABLE_NAME,cv,COLUMN_ONE+"\t="+customerId,null)>0 ;
    }
    return status;
}
You can find the complete project from github using this link.

JDK, JRE and JVM



                                                                                                               
JVM- jvm(java virtual machine) is a specification which does the following
         works.

1 . Loads code.
2 . Verifies code.
3 . Executes code.
4 . Provides Runtime environment in which byte code gets executed.



JVM internal architecture






JRE - jre acronym for java runtime environment  is an implementation of
         JVM. It contains set of libraries and other files that JVM uses at runtime.
         We can say JVM is a part of JRE.

     



JDK - JDK is the java development kit which contains development tools and
         JRE.




JVM, JRE and JDK all are platform dependent.

                                                                                             Next topic
                                                                                             Unicode system