Android-Launcher
一 Launcher的简单研究
1,什么是Launcher:Android系统启动后加载的第一个程序,是其他应用程序的入口
2,专业术语:
HomeScreen:
(workspace+hotseats)整个屏幕
shortcut快捷方式
livefolder活动文件夹
appwidget小组件
wallpaper墙纸
hotseats热底座
AllAppList:(GridView,哈希表)
二 launcher的开发类型
1,开发自己的launcher
2,在workspace中添加:
添加shortcut
添加livefolder
更改背景wallpaper
添加小组件appwidget
三 分析launcher.java的onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//实例化一些对象
LauncherApplication app = ((LauncherApplication)getApplication());
mModel = app.setLauncher(this);
mIconCache = app.getIconCache();
mDragController = new DragController(this);
mInflater = getLayoutInflater();
//实例化对象
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
mAppWidgetHost.startListening();
if (PROFILE_STARTUP) {
android.os.Debug.startMethodTracing("/sdcard/launcher");
}
//加载热底座
loadHotseats();
//检查本地化语言
checkForLocaleChange();
//设置墙纸
setWallpaperDimension();
//设置布局的layout
setContentView(R.layout.launcher);
setupViews();
//注册contentprovider的数据观察,观察它里面的数据变化
registerContentObservers();
lockAllApps();
//状态的恢复和保存
mSavedState = savedInstanceState;
restoreState(mSavedState);
if (PROFILE_STARTUP) {
android.os.Debug.stopMethodTracing();
}
if (!mRestoring) {
mModel.startLoader(this, true);
}
// For handling default keys
mDefaultKeySsb = new SpannableStringBuilder();
Selection.setSelection(mDefaultKeySsb, 0);
IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
//动态注册receiver
registerReceiver(mCloseSystemDialogsReceiver, filter);
}
四 HomeScreen可以添加的四大组件:
/**
* Handle the action clicked in the "Add to home" dialog.
*/
public void onClick(DialogInterface dialog, int which) {
Resources res = getResources();
cleanup();
switch (which) {
//添加shortcut
case AddAdapter.ITEM_SHORTCUT: {
// Insert extra item to handle picking application
pickShortcut();
break;
}
//添加Appwidget
case AddAdapter.ITEM_APPWIDGET: {
int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// start the pick activity
startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
break;
}
//添加liveforlder
case AddAdapter.ITEM_LIVE_FOLDER: {
// Insert extra item to handle inserting folder
Bundle bundle = new Bundle();
ArrayList<String> shortcutNames = new ArrayList<String>();
shortcutNames.add(res.getString(R.string.group_folder));
bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
ArrayList<ShortcutIconResource> shortcutIcons =
new ArrayList<ShortcutIconResource>();
shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
R.drawable.ic_launcher_folder));
bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
pickIntent.putExtra(Intent.EXTRA_INTENT,
new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
pickIntent.putExtra(Intent.EXTRA_TITLE,
getText(R.string.title_select_live_folder));
pickIntent.putExtras(bundle);
startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
break;
}
//改变墙纸
case AddAdapter.ITEM_WALLPAPER: {
startWallpaper();
break;
}
}
}
public void onShow(DialogInterface dialog) {
mWaitingForResult = true;
}
}
//Activity的返回值回调方法,返回的时候都需要调用
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mWaitingForResult = false;
// The pattern used here is that a user PICKs a specific application,
// which, depending on the target, might need to CREATE the actual target.
// For example, the user would PICK_SHORTCUT for "Music playlist", and we
// launch over to the Music app to actually CREATE_SHORTCUT.
if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
switch (requestCode) {
//调用的过程
case REQUEST_PICK_APPLICATION:
completeAddApplication(this, data, mAddItemCellInfo);
break;
case REQUEST_PICK_SHORTCUT:
processShortcut(data);
break;
case REQUEST_CREATE_SHORTCUT:
completeAddShortcut(data, mAddItemCellInfo);
break;
case REQUEST_PICK_LIVE_FOLDER:
addLiveFolder(data);
break;
case REQUEST_CREATE_LIVE_FOLDER:
completeAddLiveFolder(data, mAddItemCellInfo);
break;
case REQUEST_PICK_APPWIDGET:
addAppWidget(data);
break;
case REQUEST_CREATE_APPWIDGET:
completeAddAppWidget(data, mAddItemCellInfo);
break;
case REQUEST_PICK_WALLPAPER:
// We just wanted the activity result here so we can clear mWaitingForResult
break;
}
} else if ((requestCode == REQUEST_PICK_APPWIDGET ||
requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
data != null) {
// Clean up the appWidgetId if we canceled
int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
if (appWidgetId != -1) {
mAppWidgetHost.deleteAppWidgetId(appWidgetId);
}
}
}
添加shortcut的具体过程:
onClick->pickShortcut()->返回给launcher的Activity
processShortcut(Intent intent)->返回给launcher的Activity->
completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo)
UML:
下面三个与shortcut类似
livefolder
appwidget
wallpaper
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/qq_22075977/article/details/46731319