1. 程式人生 > >讓你自己寫的Android的Launcher成為系統中第一個啟動的,也是唯一的Launcher

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

檔案轉自   http://www.cnblogs.com/jqyp/archive/2012/03/04/2379362.html

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

這裡以Android2.1的原始碼為例來實際說明。

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 /** * This method is calledfrom 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裝置的廠商很有用處.

Android 2.3版本桌面功能已經很豐富了,不但支援預設5屏切換,還有抽屜、搜尋、快捷鍵、垃圾箱、appWidget等等功能,在做定製的時候,其實很多功能是用不上的。本文實現在2.3上用自己的App替換系統原有的Launch2。 1、在自己的Java App的AndroidManifest.xml中新增intent filter,DJ_HOME可換成任意其它字元
  1. <intent-filter>
  2. <action android:name="android.intent.action.MAIN" />
  3. <category android:name="android.intent.category.DJ_HOME" />
  4. <category android:name="android.intent.category.DEFAULT" />
  5. <category android:name="android.intent.category.MONKEY"/>
  6. </intent-filter>
2、修改frameworks/base/core/java/android/content/Intent.java,在L1997處新增
  1. @SdkConstant(SdkConstantType.INTENT_CATEGORY)
  2. public static final String CATEGORY_GC_HOME = "android.intent.category.DJ_HOME";
3、將如下檔案中的CATEGORY_HOME改為CATEGORY_DJ_HOME 4、刪除原來工程中自帶的packages/apps/Launch2,如果已經生成了Launch2.apk,在輸出目錄也將其刪除,重新編譯 Reference