網上有很多牛人研究 Launcher,說的都不錯,但是個人還是覺得在技術方面還是各抒己見的為好,畢竟每個人研究的面不一樣,藉此,也想為自己做個筆記。
本部落客要是基於 android2.3.7 的源碼研究 Launcher,開發工具依然使用 Eclipse(見過很多大牛直接使用文字編輯器,拋棄IDE),編譯測試環境選擇 ubuntu ,測試效果採用模擬器。
關於 Launcher 的初步介紹以及 Launcher 源碼結構,建議大家動手動眼去搜搜、看看,這裡不再贅述!
看過基本的知識之後,大家就可以下載源碼,部落格 http://blog.csdn.net/androidbluetooth/article/details/6538254 說了如何下載 android2.3.7 的源碼,然後就可以編譯源碼。
注意:源碼開發,你首先需要編譯源碼,然後才可以編譯某一個模組比如 Launcher。
長按 Home 可以彈出下面的 Dialog(圖 1)
其中有一項就是選擇 “壁紙”,當選擇之後,出現一個選取器(不是 Dialog)喲!(圖 2)
這個時候,你可以選擇是一般的壁紙,還是比較炫的動態壁紙或者是從裝置中尋找存在的照片(如果沒有還可以照相)等。
那麽代碼是如何調用的呢?看:
調用關係不算複雜,當然我們還可以使用菜單來啟用添加選項,最終還是調用
startWallpaper
方法。
那麽在這個方法中,到底發生什麼呢?研究源碼。
private void startWallpaper() { closeAllApps(true); final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER); Intent chooser = Intent.createChooser(pickWallpaper, getText(R.string.chooser_wallpaper)); // NOTE: Adds a configure option to the chooser if the wallpaper supports it // Removed in Eclair MR1// WallpaperManager wm = (WallpaperManager)// getSystemService(Context.WALLPAPER_SERVICE);// WallpaperInfo wi = wm.getWallpaperInfo();// if (wi != null && wi.getSettingsActivity() != null) {// LabeledIntent li = new LabeledIntent(getPackageName(),// R.string.configure_wallpaper, 0);// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });// } startActivityForResult(chooser, REQUEST_PICK_WALLPAPER); }
原來如此,其實是根據 Intent 調用相關的 Activity。
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);Intent chooser = Intent.createChooser(pickWallpaper, getText(R.string.chooser_wallpaper));
首先,你得搞清楚 Intent.ACTION_SET_WALLPAPER 表示什麼含義以及它的真實值,這查看 API 文檔就會明白。
public static final String ACTION_SET_WALLPAPERSince: API Level 1Activity Action: Show settings for choosing wallpaperInput: Nothing.Output: Nothing.Constant Value: "android.intent.action.SET_WALLPAPER"
該 Intent 常量是一個 String,表示啟用設定壁紙的 Activity,也就是說只要我們的系統中有這樣的 Activity(action 為
android.intent.action.SET_WALLPAPER)就可以出現在選取器中。
那麽,原生的 android 系統中有三個(從圖2可以看出)這樣的 Activity,下面細細說來!
1. WallpaperChooser.java
這是 Launcher 中的一個類,主要是選擇壁紙的操作,和 Launcher.java 在一個包下面。通過 Launcher 的 Manifest.xml 檔案就可以看到答案:
</activity><activity android:name="com.android.launcher2.WallpaperChooser" android:label="@string/pick_wallpaper" android:icon="@drawable/ic_launcher_wallpaper" android:screenOrientation="nosensor" android:finishOnCloseSystemDialogs="true"><intent-filter><action android:name="android.intent.action.SET_WALLPAPER"/><category android:name="android.intent.category.DEFAULT"/></intent-filter></activity>
2. LiveWallpaperListActivity.java
位於 /packages/wallpapers/LivePicker/src/com/android/wallpaper/livepicker 下面,主要是選擇動態壁紙。其 Manifest.xml 檔案:
<activity android:name="LiveWallpaperListActivity" android:icon="@drawable/ic_launcher_live_wallpaper" android:label="@string/live_wallpaper_picker_title" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="nosensor"> <intent-filter> <action android:name="android.service.wallpaper.LIVE_WALLPAPER_CHOOSER" /> <action android:name="android.intent.action.SET_WALLPAPER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
3. Photographs.java
在以前的版本中,android 使用的是Gallery,現在改變為 Gallery3D,位於 /packages/apps/Gallery3D/src/com/cooliris/media,對應的 Manifest.xml 檔案可自行查閱。
至此,明白了選擇壁紙所發生的故事了。
接下來,你可以按照 android opensource: 源碼開發基礎 改變代碼運行試一試了,實踐最重要!好運!
推薦幾篇好文章:
Android原理揭秘系列之一動態牆紙
Launcher修改預設壁紙(default_wallpaper)