Saturday 7 May 2016

Processes and Threads in Android

Hello Guys!!! Wishing you all Doing Well !!!!

Today I am going to discussion Process and Thread in Android application.
When an android application clicked and open by user. Then android application starts its core component in a new Linux process for the application with a single thread of execution. the only condition is that application does not have any other components running at that time. If not then it starts it component in that time running process.

                                       Example Source Code on Github
Processes
By default, all components of the same application run in the same process and most applications should not change this. However, if we find that our need to control which process a certain component belongs to, We can do so in the manifest file.
The manifest entry for each type of component element—<activity>, <service>, <receiver>, and <provider>—supports an android:process attribute that can specify a process in which that component should run.
We can set this attribute so that each component runs in its own process or so that some components share a process while others do not.
We can also set android:process so that components of different applications run in the same process—provided that the applications share the same Linux user ID and are signed with the same certificates.

Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. Android system is deciding which processes to kill, based on apps  relative importance to the user. For example, it more readily shuts down a process hosting activities that are no longer visible on screen, compared to a process hosting visible activities.
Some Important points :-

  1. The Android system tries to maintain an application process for as long as possible, 
  2. To reclaim memory for new or more important processes, it follow hierarchy system. 
  3. To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy" 
  4. Processes with the lowest importance are eliminated first from hierarchy
There are five levels in the importance hierarchy. The following list presents the different types of processes in order of importance (the first process is most important and is killed last):

Process lifecycle

  1. Foreground process
  2. Visible process
  3. Service process
  4. Background process
  5. Empty process
For detail you can click here

Threads
When an application is launched, the system creates a thread of execution for the application, called "main." This thread is very important because it is in charge of dispatching events to the appropriate user interface widgets, including drawing events. 
It is also the thread in which your application interacts with components from the Android UI toolkit (components from the android.widget and android.view packages). As such, the main thread is also sometimes called the UI thread.

 The Andoid UI toolkit is not thread-safe. So, we must not manipulate our UI from a worker thread. We  must do all manipulation to your user interface from the UI thread. Thus, there are simply two rules to Android's single thread model:
  1. Do not block the UI thread
  2. Do not access the Android UI toolkit from outside the UI thread
Android offers several ways to access the UI thread from other threads. Here is a list of methods that can help:

  1. Activity.runOnUiThread(Runnable)
  2. View.post(Runnable)
  3. View.postDelayed(Runnable, long)
  4. Handler
  5. AsyncTask


Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...