Android 如何將定製的Launcher成爲系統中唯一的Launcher

 如果你要定製一個Android系統,你想用你自己的Launcher(Home)作主界面來替換Android自己的Home,而且不希望用戶安裝的Launcher來替換掉你的Launcher.

我們可以通過修改Framework來實現這樣的功能。

這裏以Android2.1的源代碼爲例來實際說明。

1)首先了解一下Android的啓動過程。
  Android系統的啓動先從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.
從上面的註釋可以看到:init1這個方法時被Zygote調用來初始化系統的,init1會啓動native的服務如SurfaceFlinger,AudioFlinger等等,這些工作做完以後會回調init2來啓動Android的service。

這裏我們主要來關注init2的過程。
init2中啓動ServerThread線程,
ServerThread中啓動了一系列的服務,比如這些:

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後開始開始啓動Launcher。

在尋找Launcher的時候是根據HOME的filter(在Manifest中定義的<category android:name=”android.intent.category.HOME” />)來過濾。
然後根據filter出來的HOME來啓動,如果只有一個HOME,則啓動這個HOME,如果用戶自己裝了HOME,那就會彈出來一個列表供用戶選擇。

我們現在希望從這裏彈出我們自己定製的Launcher,同時也不希望彈出選擇HOME的界面,我們不希望用戶修改我們的home,比如我們的home上放了好多廣告,以及強制安裝的程序,不希望用戶把它幹掉。

我們可以通過這樣來實現:

2) 定義一個私有的filter選項,然後用這個選項來過濾HOME.
   一般情況下我們使用Manifest中定義的<category android:name=”android.intent.category.HOME”來過濾的,我們現在增加一個私有的HOME_FIRST過濾。

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

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

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

    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);
   改成 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>

然後將編譯好的apk放到/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

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

這樣我們啓動的模擬器裏面用的p_w_picpath就是我們剛纔編譯好的自己定製的東西了。
從模擬器上可以看到啓動的Launcher是我們自己的Launcher,不會出現默認的Launcher了,也不會出現選擇界面。

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

這樣我們就牢牢控制了用戶的桌面。
只有我們自己定製的HOME才能裝上。 這對於定製Android設備的廠商很有用處。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章