中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

讓(rang)你自己寫的(de)(de)Android的(de)(de)Launcher成(cheng)為系統中第一(yi)個啟(qi)動(dong)的(de)(de),也(ye)是(shi)唯一(yi)的(de)(de)Launcher

如果你要定制一個Android系統,你想用你自己的Launcher(Home)作主界面來替換Android自己的Home,而且不希望用戶安裝的Launcher來替換掉你的Launcher.
我們可以通(tong)過修改Framework來實(shi)現這(zhe)樣(yang)的(de)功能。

這里以(yi)Android2.1的(de)源代碼為例來實(shi)際說明。

1)首先了解一下Android的啟動過程。
  Android系統的啟動先從(cong)Zygote開始啟動,然后......(中間的過程就不說了).....一直到了SystemServer(framework)這個地方,看到這段代碼:

/**
     * 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();
    }
}

 

 

從SystemServer的main函數開始啟動各種服務。
首先啟動init1,然后啟動init2.
從上面(mian)的(de)(de)(de)注釋可以(yi)看到:init1這(zhe)個方(fang)法時被Zygote調用(yong)來初(chu)始化系統(tong)的(de)(de)(de),init1會(hui)啟(qi)動native的(de)(de)(de)服務如SurfaceFlinger,AudioFlinger等等,這(zhe)些工作做完以(yi)后(hou)會(hui)回調init2來啟(qi)動Android的(de)(de)(de)service。

這里我們主要來關注init2的過程。
init2中啟動ServerThread線程,
ServerThread中啟動了一(yi)系列的服務(wu),比如這些:

ActivityManagerService
EntropyService
PowerManagerService
TelephonyRegistry
PackageManagerService
AccountManagerService
BatteryService
HardwareService
Watchdog
SensorService
BluetoothService
StatusBarService
ClipboardService
InputMethodManagerService
NetStatService
ConnectivityService
AccessibilityManagerService
NotificationManagerService
MountService
DeviceStorageMonitorService
LocationManagerService
SearchManagerService
FallbackCheckinService
WallpaperManagerService
AudioService
BackupManagerService
AppWidgetService

這些大大小小的服務起來以后,開始
((ActivityManagerService)ActivityManagerNative.getDefault()).systemReady()
在systemReady后(hou)開始(shi)開始(shi)啟(qi)動Launcher。

在尋找Launcher的時候是根據HOME的filter(在Manifest中定義的<category android:name="android.intent.category.HOME" />)來過濾。
然后(hou)根(gen)據filter出(chu)來的(de)HOME來啟動(dong),如(ru)(ru)果只有一個(ge)HOME,則啟動(dong)這個(ge)HOME,如(ru)(ru)果用戶自(zi)己裝了HOME,那就會彈出(chu)來一個(ge)列表供用戶選擇(ze)。

我們(men)現在希(xi)望(wang)從這里彈出我們(men)自(zi)己定制(zhi)(zhi)的(de)(de)Launcher,同時也不希(xi)望(wang)彈出選擇HOME的(de)(de)界(jie)面,我們(men)不希(xi)望(wang)用戶(hu)修(xiu)改(gai)我們(men)的(de)(de)home,比如(ru)我們(men)的(de)(de)home上放了(le)好多廣告(gao),以及(ji)強制(zhi)(zhi)安裝的(de)(de)程序,不希(xi)望(wang)用戶(hu)把它干掉。

我(wo)們可以(yi)通過這樣來實(shi)現:

2) 定義一個私有的filter選項,然后用這個選項來過濾HOME.
   一般情況下我(wo)們使(shi)用Manifest中定義(yi)的(de)(de)<category android:name="android.intent.category.HOME"來過濾(lv)的(de)(de),我(wo)們現在(zai)增加(jia)一個私有的(de)(de)HOME_FIRST過濾(lv)。

 &nbsp;&nbsp;  在Intent.java(frameworks/base/core/java/android/content/Intent.java)中(zhong)添加兩行代碼

    //lixinso:添加CATEGORY_HOME_FIRST
    @SdkConstant(SdkConstantType.INTENT_CATEGORY)
    public static final String CATEGORY_HOME_FIRST = "android.intent.category.HOME_FIRST";

3)修改(gai)和(he)CATEGORY_HOME相關的所有(you)的地方,都改(gai)成HOME_FIRST,主要是framework中(zhong)的這幾個(ge)地方:

    frameworks/base/services/java/com/android/server/am/ActivityManagerService.java中
    //intent.addCategory(Intent.CATEGORY_HOME);
    改成intent.addCategory(Intent.CATEGORY_HOME_FIRST); //lixinso:
    //if (r.intent.hasCategory(Intent.CATEGORY_HOME)) {
    改成if (r.intent.hasCategory(Intent.CATEGORY_HOME_FIRST)) { //lixinso: Intent.CATEGORY_HOME -> Intent.CATEGORY_HOME_FIRST
   frameworks/base/services/java/com/android/server/am/HistoryRecorder.java中
   // _intent.hasCategory(Intent.CATEGORY_HOME) &&
   改成 _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);
   改成 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);
   改成 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);
   改成(cheng) 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);
   改成 ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME_FIRST),0); //lixinso

4) 寫一個自己的Launcher.
   可以參考android sample中的Launcher,或者android源代碼中的 /packages/apps/Launcher 來寫。
   在Launcher中標記其是不是Launcher的最關鍵的代碼時Manifest中的filter:android:name="android.intent.category.HOME"
   現在我們定義了自己的filter,那么,我們在我們自己寫的Launcher中將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>

然后(hou)將(jiang)編譯好的apk放到(dao)/out/target/product/generic/system/app目錄下。

5)將Android自帶的Launcher刪除掉,包括源代碼(packages/apps/Launcher)和apk(/out/target/product/generic/system/app/Launcher.apk)。

6)
做完這些工作,就可以重新編譯Android了,我們可以編譯修改過的幾個相關的包。
如果之前編譯過了Android源碼,可以用mmm命令來編譯部分的改動。
這里需要這樣編譯:

$ . build/envsetup.sh
$ mmm frameworks/base
$ mmm frameworks/base/services/java
$ mmm frameworks/policies/base/mid
$ mmm frameworks/policies/base/phone

7)
編譯完成后重新生成img文件。
$ make snod

8) 現在可以啟動Android模擬器來看效果了。
首先設置環境變量:
$ export ANDROID_PRODUCT_OUT= ./out/target/product/generic
然后切換到
$ cd ./out/host/linux-x86/bin
運行
$ ./emulator

這樣我們啟動的模擬器里面用的image就是我們剛才編譯好的自己定制的東西了。
從模擬(ni)器上可以看到啟動的(de)Launcher是我們自(zi)己的(de)Launcher,不(bu)會(hui)出現(xian)默認的(de)Launcher了,也不(bu)會(hui)出現(xian)選擇界面。

9)我們再驗證一下,如果用戶裝上了一個其他的Launcher(Home)會怎么樣。
  從網上找一個一般的Launcher或者自己寫一個一般的Launcher裝上去,重新啟動,不會出現選擇界面。
  按HOME鍵(jian)也(ye)不會出來兩個HOME來選擇。

這樣我們就牢牢控制了用戶的桌面。
只有我們自己定制的(de)HOME才能(neng)裝上。 這對于定制Android設(she)備的(de)廠(chang)商很有用處.

posted on 2012-03-04 16:47  ①塊腹肌  閱讀(28486)  評論(0)    收藏  舉報