Android boot process

Boot process of embedded system is similar to PC from overview level, but slightly different from microscopic level.

Here’s the boot process of an Android device:

  1. PC-BIOS/BootMonitor, MBR and GRUB/LILO etc are all combined in one Boot Strap Firmwarecalled Bootloader. Its init.S initializes stacks, zeros the BSS segment and calls _main() in main.c. The main.c initializes hardware (clocks, board, keypad, console) and creates Linux tags which in turns loads Linux Kernel image in memory.
  2. Linux Kernel sets up the system, loads drivers and starts running the first process init.
  3. The init process involves setup of File System (mount points of /sys etc are created at this point) and execution of init.rc.
  4. Execution of init.rc: It is boot-up script which uses Android-Specific syntax. Zygot process in init.rc brings up Dalvik VM and starts the system server.

All these are happened before Boot Animation is displayed.

For more details of processes,, read below

The Android boot process from power on

Since mobile platforms and embedded systems has some differences compared to Desktop systems in how they initially start up and boot this post will discuss the initial boot stages of an Android phone in some detail. Since we have used the Beagle Board as reference in some previous examples any specifics here are related to a similar system.

1. Power on and boot ROM code execution
At power on the CPU will be in a state where no initializations have been done. Internal clocks are not set up and the only memory available is the internal RAM. When power supplies are stable the execution will start with the Boot ROM code. This is a small piece of code that is hardwired in the CPU ASIC. For more information on boot ROM and configurations study the initalization chapter in
the Omap 3530 TRM.

  • A. The Boot ROM code will detect the boot media using a system register that maps to some physical balls on the asic. This is to determine where to find the first stage of the boot loader.
  • B. Once the boot media sequence is established the boot ROM will try to load the first stage boot loader to internal RAM. Once the boot loader is in place the boot ROM code will perform a jump and execution continues in the boot loader.

2. The boot loader
The boot loader is a special program separate from the Linux kernel that is used to set up initial memories and load the kernel to RAM. On desktop systems the boot loaders are programs like GRUB and in embedded Linux uBoot is often the boot loader of choice. Device manufacturers often use their own proprietary boot loaders. The requirements on a boot loader for Linux running on an ARM system can be found in the Booting document under /Documentation/arm in the kernel source tree.

  • A. The first boot loader stage will detect and set up external RAM.
  • B. Once external RAM is available and the system is ready the to run something more significant the first stage will load the main boot loader and place it in external RAM.
  • C. The second stage of the boot loader is the first major program that will run. This may contain code to set up file systems, additional memory, network support and other things. On a mobile phone it may also be responsible for loading code for the modem CPU and setting up low level memory protections and security options.
  • D. Once the boot loader is done with any special tasks it will look for a Linux kernel to boot. It will load this from the boot media (or some other source depending on system configuration) and place it in the RAM. It will also place some boot parameters in memory for the kernel to read when it starts up.
  • E. Once the boot loader is done it will perform a jump to the Linux kernel, usually some decompression routine, and the kernel assumes system responsibility.

3. The Linux kernel
The Linux kernel starts up in a similar way on Android as on other systems. It will set up everything that is needed for the system to run. Initialize interrupt controllers, set up memory protections, caches and scheduling.

  • A. Once the memory management units and caches have been initialized the system will be able to use virtual memory and launch user space processes.
  • B. The kernel will look in the root file system for the init process (found under system/core/init in the Android open source tree) and launch it as the initial user space process.

4. The init process
The init process is the “grandmother” of all system processes. Every other process in the system will be launched from this process or one of its descendants.

  • A. The init process in Android will look for a file called init.rc. This is a script that describes the system services, file system and other parameters that need to be set up. The init.rc script is placed in system/core/rootdir in the Android open source project.
  • B. The init process will parse the init script and launch the system service processes.

5. Zygote and Dalvik
The Zygote is launched by the init process and will basically just start executing and and initialize the Dalvik VM.
6. The system server
The system server is the first java component to run in the system. It will start all the Android services such as telephony manager and bluetooth. Start up of each service is currently written directly into the run method of the system server. The system server source can be found in the file frameworks/base/services/java/com/android/server/SystemServer.java in the open source project.

FROM HERE

android get wifi connection states

in my app, i need to notify user before they connect to internet for video if their device is not connected to wifi.

To achieve this, we need to add ACCESS_NETWORK_STATE permission in the Manifest file first. you will obviously also need the INTERNET permission.

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
    <uses-permission android:name="android.permission.INTERNET"/> 

Then we check the state:

   private void openSearch(final String target) {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo.State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
            navigateToSearch(target);
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setIcon(android.R.drawable.ic_dialog_alert);
            builder.setTitle(R.string.alert_title);
            builder.setMessage(R.string.alert_message);
            builder.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // if user still want to continue, GO. 
                    navigateToSearch(target);
                }
            });
            builder.setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.create();
            builder.show();
        }
    }

    private void navigateToSearch(String target){
        Intent intent = new Intent(this, VideoSearch.class);
        intent.putExtra(SEARCH_TARGET, target);
        intent.putExtra(MainActivity.SONG_NAME, songName);
        startActivity(intent);
    }

save settings in android app

1. settings

use getSharedPreferences(prefFileNameString, mode).

        sharedPreferences = getSharedPreferences(getString(R.string.app_pref), MODE_PRIVATE);
        if (sharedPreferences.getBoolean(getString(R.string.night_mode_pref_key), true)) {
            setTheme(android.R.style.Theme_Holo);
        } else {
            setTheme(android.R.style.Theme_Holo_Light);
        }

I just need 1 settings file in my current app, so I only need to define a string in resource for the “prefFileNameString” and use it across all activities.

2. change theme

The above code is used for switch between holo and holo light theme. Make sure they are before your setContentView method.

3.add hr

About adding a hr in the view, we can use the following setting:

<!--add a separate line in between-->
    <View android:layout_width="fill_parent"
          android:layout_height="1dp"
          android:background="@android:color/darker_gray"/>

parse json file in android from asset

1. The JSON Structure

I am taking an example of following JSON which will give you list of employees and each employee will have details like id, name, city, gender, age etc.

{
"employee": [
{
"id": 101,
"name": "Amar",
"city": "Dausa",
"gender": "M",
"age": 21
},
{
"id": 102,
"name": "Sunil",
"city": "Bharatpur",
"gender": "M",
"age": 22
},
{
"id": 103,
"name": "Uday",
"city": "Bharatpur",
"gender": "M",
"age": 22
},

{
"id": 104,
"name": "Rahul",
"city": "Alwar",
"gender": "M",
"age": 21
}
]
}

Consider that above JSON data is stored in jsondata.txt file which is stored in assetsfolder.

2. Reading Text File (from assets folder)

Before we start parsing the above JSON data, first we need to store data from jsondata.txtfile to a string. Here is the code snippet-

// Reading text file from assets folder
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"jsondata.txt")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}

String myjsonstring = sb.toString();

 3. Parsing JSON data from String


In second step we store JSON data from jsondata.txt file to a string called myjsonstring, Now we are ready to parse JSON data from it. Here is the code snippet-

// Try to parse JSON
try {
// Creating JSONObject from String
JSONObject jsonObjMain = new JSONObject(myjsonstring);

// Creating JSONArray from JSONObject
JSONArray jsonArray = jsonObjMain.getJSONArray("employee");

// JSONArray has four JSONObject
for (int i = 0; i &lt; jsonArray.length(); i++) {

// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);

// Getting data from individual JSONObject
int id = jsonObj.getInt("id");
String name = jsonObj.getString("name");
String city = jsonObj.getString("city");
String gender = jsonObj.getString("gender");
int age = jsonObj.getInt("id");

}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

FROM HERE

create a quick android app with rss

现在很多的网站都推出了自己的app,但是对于我们这些普通的小网站来说,自己去做一个app有点得不尝试,也没有去那么的精力去做这些,于是就有人做了这样的服务,AppYet就是这样的而一个服务,只要提供网站的feed rss地址,appyet就能自动的生成一个比较perfect的app,只是对于免费的用户, 嵌套了广告,毕竟是免费的,我们也就没有什么可以说的。

appyet是提供了一种服务,只需你提供网站的feed rss,就能很快的生成一个网站的app出来,对于小网站用户来说足矣。(from this blog

对于webvanta, 提供了rss生成script

rss2.0的格式:

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <rss version=”2.0″>
  3. <channel>
  4.     <title>DotNetBips.com Latest Articles</title>
  5.     <link>www.dotnetbips.com</link>
  6.     <description>DotNetBips.com Latest Articles</description>
  7.     <language>zh-cn</language>
  8.     <copyright>Copyright (C) DotNetBips.com. All rights reserved.</copyright>
  9.     <generator>www.dotnetbips.com RSS Generator</generator>
  10.     <item>
  11.     <title>Using WebRequest and WebResponse</title>
  12.     <link>http://www.dotnetbips.com/displayarticle.aspx?id=239</link&gt;
  13.     <description>Description here</description>
  14.     <category></category>
  15.     <author>Bipin Joshi</author>
  16.     <copyright></copyright>
  17.     <pubDate>Sun, 25 Jan 2004 12:00:00 AM GMT</pubDate>
  18.     </item>
  19. </channel>
  20. </rss>

rss:  每个RSS Feed,都有而且只能有一个rss标签,作为顶层元素,属性version是版本号,如2.0
channel:   在rss标签下,必须有且只能有一个channel标签,可包含