讓你自己寫的Android的Launcher成為系統中第一個啟動的,也是唯一的Launcher

來源:互聯網
上載者:User

如果你要定製一個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

8) 現在可以啟動Android模擬器來看效果了。

首先設定環境變數:

$ export ANDROID_PRODUCT_OUT= ./out/target/product/generic

然後切換到

$ cd ./out/host/linux-x86/bin

運行

$ ./emulator

這樣我們啟動的模擬器裡面用的image就是我們剛才編譯好的自己定製的東西了。

從模擬器上可以看到啟動的Launcher是我們自己的Launcher,不會出現預設的Launcher了,也不會出現選擇介面。

9)我們再驗證一下,如果使用者裝上了一個其他的Launcher(Home)會怎麼樣。

  從網上找一個一般的Launcher或者自己寫一個一般的Launcher裝上去,重新啟動,不會出現選擇介面。

  按HOME鍵也不會出來兩個HOME來選擇。

這樣我們就牢牢控制了使用者的案頭。

只有我們自己定製的HOME才能裝上。 這對於定製Android裝置的廠商很有用處.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.