Launcher: 设置壁纸_源码跟踪

来源:互联网 发布:手机木马软件监听器 编辑:程序博客网 时间:2024/05/16 15:20

网上有很多牛人研究 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)