最近组织上派遣了我一个任务,最终目的只有一个,就是看看launcher你的wallpaper设置的流程。
废话不多说,直接看源码。
tips:在com.android.launcher2.Launcher类内有一个方法叫做startWallpaper(),很明显此方法就是设置壁纸的关键所在。
要说我是怎么知道的,大概老外起名字也不是随便起的,要是找类似的功能,比如创建快捷方式,在Launcher类里
按快捷键CTRL+O 键入对应的英文比如(*shortcut*),列出来的方法看名字也能猜出来对吧。同上,wallpaper就是这么找到的。
private void startWallpaper() {
showWorkspace(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的chooser静态方法没有使用过,没有关系,看对应的api就是了。
Convenience function for creating a ACTION_CHOOSER Intent.
Builds a new ACTION_CHOOSER Intent that wraps the given target intent, also optionally supplying a title. If the target intent has specifiedFLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION, then these flags will also be set in the returned chooser intent, with its ClipData set appropriately: either a direct reflection of getClipData() if that is non-null, or a new ClipData build from getData().
target | The Intent that the user will be selecting an activity to perform. |
---|---|
title | Optional title that will be displayed in the chooser. |
因此顺藤摸瓜,看看设置壁纸是如何实现的。找到WallpaperChooser类,查看源码
public class WallpaperChooser extends Activity {
private static final String TAG = "Launcher.WallpaperChooser";
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.wallpaper_chooser_base);
Fragment fragmentView =
getFragmentManager().findFragmentById(R.id.wallpaper_chooser_fragment);
Log.i(TAG, "wallpaper"+(fragmentView==null));
// TODO: The following code is currently not exercised. Leaving it here in case it
// needs to be revived again.
if (fragmentView == null) {
/* When the screen is XLarge, the fragment is not included in the layout, so show it
* as a dialog
*/
DialogFragment fragment = WallpaperChooserDialogFragment.newInstance();
fragment.show(getFragmentManager(), "dialog");
}
}
}
事实上在对应的layout上有如下
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.android.launcher2.WallpaperChooserDialogFragment"
android:id="@+id/wallpaper_chooser_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
实际是使用了WallpaperChooserDialogFragment,注释说了如果是Xlarge屏幕,没有显示该fragment,则获得WallpaperChooserDialogFragment对象
此时也就是设置了对应的壁纸。
android 壁纸设置分析http://blog.csdn.net/q13073451412/article/details/9063283