Customize an android launcher (home)

Source: Internet
Author: User
Tags call back

If you want to customize an Android system, you want to use your own launcher (home) as the main interface to replace your android home, you do not want to replace the launcher installed by the user.
You can modify the framework to implement this function.

Here we use the source code of android2.1 as an example to describe it.

1) First, let's take a look at the android startup process.
Start android from zygote, and then ...... (the process in the middle won't be mentioned )..... go to systemserver (framework), and see the code:

/**
* This method is called from zygote to initialize the system. This will cause the native
* Services (surfaceflinger, audioflinger, Etc ..) to be started. After that it will call back
* Up into init2 () to start the android services.
*/
Native public static void init1 (string [] ARGs );

Public static void main (string [] ARGs ){
If (samplingprofilerintegration. isenabled ()){
Samplingprofilerintegration. Start ();
Timer = new timer ();
Timer. Schedule (New timertask (){
@ Override
Public void run (){
Samplingprofilerintegration. writesnapshot ("system_server ");
}
}, Snapshot_interval, snapshot_interval );
}

// The system server has to run all of the time, so it needs to be
// As efficient as possible with its memory usage.
Vmruntime. getruntime (). settargetheaputilization (0.8f );

System. loadlibrary ("android_servers ");
Init1 (ARGs );
}

Public static final void init2 (){
Log. I (TAG, "entered the Android system server! ");
Thread thr = new serverthread ();
Thr. setname ("android. server. serverthread ");
Thr. Start ();
}
}

Start various services from the main function of systemserver.
Start init1, and then start init2.
From the preceding notes, we can see that the init1 method is called by zygote to initialize the system. init1 will start native services such as surfaceflinger and audioflinger, after these tasks are completed, init2 will be called back to start the android service.

Here we mainly focus on the init2 process.
Start the serverthread thread in init2,
Serverthread starts a series of services, such:

Activitymanagerservice
Entropyservice
Powermanagerservice
Telephonyregistry
Packagemanagerservice
Accountmanagerservice
Batteryservice
Hardwareservice
Watchdog
Sensorservice
Bluetoothservice
Statusbarservice
Clipboardservice
Inputmethodmanagerservice
Netstatservice
Connectivityservice
Accessibilitymanagerservice
Icationicationmanagerservice
Mountservice
Devicestoragemonitorservice
Locationmanagerservice
Searchmanagerservice
Fallbackcheckinservice
Wallpapermanagerservice
Audioservice
Backupmanagerservice
Appwidgetservice

After these services are started
(Activitymanagerservice) activitymanagernative. getdefault (). systemready ()
Start launcher after systemready.

When looking for launcher, filter it based on the filter of home (<category Android: Name = "android. Intent. Category. Home"/> defined in manifest.
Start with the filter-out home. If there is only one home, start this home. If the user installs the home, a list is displayed for the user to choose.

We now want to bring up our own custom launcher from here, and do not want to bring up the home selection interface. We do not want users to modify our home, for example, we put a lot of advertisements on our home and forced installation programs, and we do not want users to take them out.

We can achieve this through:

2) define a private filter option and use this option to filter the home.
In general, we use the <category Android: Name = "android. Intent. Category. Home" defined in manifest to filter. Now we add a private home_first filter.

Add two lines of code in intent. Java (frameworks/base/CORE/Java/Android/content/intent. Java)

// Lixinso: Add category_home_first
@ Sdkconstant (sdkconstanttype. intent_category)
Public static final string category_home_first = "android. Intent. Category. home_first ";

3) modify all the places related to category_home to home_first, mainly in the framework:

In frameworks/base/services/Java/COM/Android/Server/AM/activitymanagerservice. Java
// Intent. addcategory (intent. category_home );
Change intent. addcategory (intent. category_home_first); // lixinso:
// If (R. Intent. hascategory (intent. category_home )){
Change to If (R. Intent. hascategory (intent. category_home_first) {// lixinso: intent. category_home-> intent. category_home_first

In frameworks/base/services/Java/COM/Android/Server/AM/historyrecorder. Java
// _ Intent. hascategory (intent. category_home )&&
Change to _ intent. hascategory (intent. category_home_first) & // lixinso: intent. category_home-> intent. category_home_first

Frameworks/policies/base/Mid/COM/Android/Internal/policy/impl/midwindowmanager. Java
// Mhomeintent. addcategory (intent. category_home );
Change to mhomeintent. addcategory (intent. category_home_first); // lixinso

Frameworks/policies/base/Mid/COM/Android/Internal/policy/impl/recentapplicationsdialog. Java
// New intent (intent. action_main). addcategory (intent. category_home), 0 );
Change to new intent (intent. action_main). addcategory (intent. category_home_first), 0); // lixinso

Frameworks/policies/base/phone/COM/Android/Internal/policy/impl/phonewindowmanager. Java
// Mhomeintent. addcategory (intent. category_home );
Change to mhomeintent. addcategory (intent. category_home_first); // lixinso

Frameworks/policies/base/phone/COM/Android/Internal/policy/impl/recentapplicationsdialog. Java
// Resolveinfo homeinfo = PM. resolveactivity (new intent (intent. action_main). addcategory (intent. category_home), 0 );
Change to resolveinfo homeinfo = PM. resolveactivity (new intent (intent. action_main). addcategory (intent. category_home_first), 0); // lixinso

4) write your own launcher.
Refer to launcher in the android sample or/packages/apps/launcher in the android source code.
Filter: Android: Name = "android. Intent. Category. Home" in manifest when you mark whether it is the most critical code of launcher"
Now we have defined our own filter, So we changed manifest:
<Application Android: Process = "android. process. acore3" Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = ". firstappactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. home_first"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Category Android: Name = "android. Intent. Category. Monkey"/>
</Intent-filter>
</Activity>
</Application>

Then, put the compiled apk in the/out/target/product/generic/system/APP directory.

5) Delete the built-in Android launcher, including the source code (packages/apps/launcher) and APK (/out/target/product/generic/system/APP/launcher.apk ).

6)
After doing this, you can re-compile Android. We can compile several modified packages.
If you have compiled the android source code before, you can use the mmm command to compile some changes.
Compile as follows:

$. Build/envsetup. Sh
$ Mmm frameworks/base
$ Mmm frameworks/base/services/Java
$ Mmm frameworks/policies/base/Mid
$ Mmm frameworks/policies/base/phone

7)
After compilation, the imgfile is regenerated.
$ Make Snod

8) Now you can start the android simulator to see the effect.
First, set the environment variable:
$ Export android_product_out =./out/target/product/generic
Switch
$ Cd./out/host/linux-x86/bin.
Run
$./Emulator

In this way, the image used in the simulator we started is something we just compiled and customized.
From the simulator, we can see that the started launcher is our own launcher. The default launcher will not appear, nor the selection interface will appear.

9) Let's verify what happens if a user installs another launcher (home.
Find a general launcher on the Internet or write a general launcher on your own. Restart the device and the selection interface will not appear.
If you press the Home Key, neither home nor home is selected.

In this way, we firmly control the user's desktop.
Only the custom home can be installed. This is very useful for manufacturers who customize Android devices.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.