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
Advertisements

Android webview crashes with Fatal signal 11 (SIGSEGV) when back button is hit

I am using html5webview in my app to display online video from various sites.

It is a good tool with nice little piece of code enables video played in a overlay. The reason I use it is for some reason, the video just does not move in standard android webview.

 

Everything works fine except it always crashes if I enter the webview, then click the back button without touching any part of it.

The error I got is:  Fatal signal 11 (SIGSEGV), which is a memory issue that I can not debug.

Did a lot of search in google.

It turns out that if I remove the

  1. setUseWideViewPort(true)
  2. setLoadWithOverviewMode(true)

of the WebSettings in the webview, everything goes back to normal.

I have no idea why it happens. Some explaination:

setLoadWithOverviewMode(true) loads the WebView completely zoomed out

setUseWideViewPort(true) makes the Webview have a normal viewport (such as a normal desktop browser), while when false the webview will have a viewport constrained to its own dimensions (so if the webview is 50px*50px the viewport will be the same size)

 

 

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);
    }

change the three dots action bar overflow menu icon in android

To override the icon, we would need a new theme that extends the Holo or HoloLight or… For my app, since I support changing theme, i would need 2 custom theme.
These code can be added in the res/values/xxx.xml, i just put in my strings.xml

    <style name="MyHoloTheme" parent="@android:style/Theme.Holo">
        <item name="android:actionOverflowButtonStyle">@style/OverFlow</item>
    </style>

    <style name="MyHoloLightTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionOverflowButtonStyle">@style/OverFlow</item>
    </style>

    <style name="OverFlow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
        <item name="android:src">@drawable/video_icon</item>
    </style>

if you just have 1 theme, just set the custom theme in the activity definition.

        <activity android:theme="@style/MyHoloLightTheme"
                  android:name=".DisplayLyricActivity"
                  android:label="歌词"
                  android:parentActivityName="MainActivity"/>

I need 2 themes, if you also need 2 or more, just switch it in the code:

        SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_pref), MODE_PRIVATE);
        if (sharedPreferences.getBoolean(getString(R.string.night_mode_pref_key), true)) {
            setTheme(R.style.MyHoloTheme);
        } else {
            setTheme(R.style.MyHoloLightTheme);
        }

See this post for more about using sharedPreference for user settings.

refresh android spinner

If you change the underlying “back” list for the spinner, the spinner does not refresh automatically.
What we can do is :

  ....some change to you backing list 
  BaseAdapter adapter = (BaseAdapter) yourSpinner.getAdapter();
  adapter.notifyDataSetChanged();

The notifyDataSetChanged() will force refresh the spinner so that the list change can be applied to the view.

refresh activity on back button in android

Normally, when user click the back button in android, the next view is not refreshed. I had this problem when I was doing the settings. if user changed the some settings and hit back button, the new settings won’t be applied to the “back” list view since all the cells are reused.
you can do some scroll to refresh the list view but that is definitely not something we wanna user to do.
first solution is to override the onResume method in the back view, but it is not guaranteed to work in listView.
However the following code should do the work:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && isNightModeToggled) {
            Intent a = new Intent(this,MainActivity.class);
            a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(a);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

the “MainActivity” is my next view, replaced it with you own “next” view.

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"/>