(1)      Launcher的定义:Android系统启动后,加载的第一个程序,是其他应用程序的入口。

(2)      Launcher的构成:HomeScreen(1.workspace(AppWidget,Wallpaper,Livefolder,ShortCut),2.HotSeats,3.AllApps/allApplist:GridView)

(3)      针对Launcher的开发类型:

1.      开发自定义的Launcher

2.      与Workspace相关的开发(AppWidget,Wallpaper,Livefolder,ShortCut)

(4)      解读Launcher源代码(以ShortCut为例),Launcher为系统级应用,位于Applications层。源代码位于MyAndroid\packages\apps\Launcher2中,可讲它导入eclipse中。

(5)      看Manifest文件可知,Launcher的主Activity为:com.android.launcher2.Launcher。下面是它的部分配置信息:

<activity
android:name="com.android.launcher2.Launcher"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
</intent-filter>
</activity>

(6)      关于Activity的四种加载模式(android:launchMode):在多Activity开发中,有可能是自己应用之间的Activity跳转,或者夹带其他应用的可复用Activity。有可能会希望跳转到原来某个Activity实例,而不是产生大量重复的Activity。这就需要为Activity配置特定的加载模式,而不是默认的加载模式。

1.      Standard:默认模式,每次激活Activity时都会创建Activity,并放入任务栈中。

2.      singleTop:如果在任务栈栈顶正好存在该Activity的实例,就重用该实例(会调用实例的onNewIntent()),否则就会创建新的实例并放入栈顶,即使栈中已经存在该Activity的实例。

3.      singleTask:如果在栈中已经有该Activity的实例,就会重用该实例,重用时,会让该实例回到栈顶,因此在它上面的实例将会被移出栈。

4.      singleInstance:在一个新栈中创建该Activity的实例,并让多个应用共享该栈中的该Activity实例。一旦该模式的Activity实例已经存在于某个栈中,任何应用再激活该Activity时都会重用该栈中的实例( 会调用实例的onNewIntent() )。其效果相当于多个应用共享一个应用,不管谁激活该 Activity 都会进入同一个应用中。

(7)      当用户按菜单键中的添加按钮或者长按workspace中的空白区域,会弹出一个对话框(如下图所示):

@Override
publicbooleanonOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
caseMENU_ADD:
addItems(); /* 按菜单键,添加按钮*/
returntrue;
caseMENU_MANAGE_APPS:
manageApps();
returntrue;
caseMENU_WALLPAPER_SETTINGS:
startWallpaper();
returntrue;
caseMENU_SEARCH: @Override
publicbooleanonOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
caseMENU_ADD:
addItems(); /* 按菜单键,添加按钮*/
returntrue;
caseMENU_MANAGE_APPS:
manageApps();
returntrue;
caseMENU_WALLPAPER_SETTINGS:
startWallpaper();
returntrue;
caseMENU_SEARCH:
onSearchRequested();
returntrue;
caseMENU_NOTIFICATIONS:
showNotifications();
returntrue;
} returnsuper.onOptionsItemSelected(item);
} privatevoidaddItems() {
closeAllApps(true);
showAddDialog(mMenuAddInfo); /* 显示对话框 */
} // 长按workspace空白区域
if (mWorkspace.allowLongPress()) {
if (cellInfo.cell == null) {
if (cellInfo.valid) {
// User long pressed on empty space
mWorkspace.setAllowLongPress(false);
mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
showAddDialog(cellInfo);/* 显示对话框 */
}
} else {
if (!(cellInfo.cellinstanceof Folder)) {
// User long pressed on an item
mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
mWorkspace.startDrag(cellInfo);
}
}
}
// showAddDialog方法
privatevoidshowAddDialog(CellLayout.CellInfocellInfo) {
mAddItemCellInfo = cellInfo;
mWaitingForResult = true;
showDialog(DIALOG_CREATE_SHORTCUT);
} // showDialog方法会调用下面这个方法创建对话框
protected Dialog onCreateDialog(int id) {
switch (id) {
caseDIALOG_CREATE_SHORTCUT: // 注意这里的常量
returnnewCreateShortcut().createDialog();
caseDIALOG_RENAME_FOLDER:
returnnewRenameFolder().createDialog();
} returnsuper.onCreateDialog(id);
} // CreateShortcut为Launcher的内部类,看其中的pickShortcut()
// 方法,它实现很多接口 /**
* Displays the shortcut creation dialog and launches, if necessary, theappropriate activity.
*/
privateclassCreateShortcutimplementsDialogInterface.OnClickListener,
DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
DialogInterface.OnShowListener { privateAddAdaptermAdapter; Dialog createDialog() {
mAdapter = newAddAdapter(Launcher.this); finalAlertDialog.Builder builder = newAlertDialog.Builder(Launcher.this);
builder.setTitle(getString(R.string.menu_item_add_item));
builder.setAdapter(mAdapter, this); builder.setInverseBackgroundForced(true); AlertDialog dialog = builder.create();
dialog.setOnCancelListener(this);
dialog.setOnDismissListener(this);
dialog.setOnShowListener(this); return dialog;
} publicvoidonCancel(DialogInterface dialog) {
mWaitingForResult = false;
cleanup();
} publicvoidonDismiss(DialogInterface dialog) {
} privatevoid cleanup() {
try {
dismissDialog(DIALOG_CREATE_SHORTCUT);
} catch (Exception e) {
// An exception is thrown if the dialog is not visible, which is fine
}
} /**
* Handle the action clicked in the “Add to home” dialog.
*/
publicvoidonClick(DialogInterface dialog, int which) {
Resources res = getResources();
cleanup(); switch (which) {
caseAddAdapter.ITEM_SHORTCUT: {
// Insert extra item to handle picking application
pickShortcut(); //当点击对话框中的快捷方式项,会//调用此方法
break;
} caseAddAdapter.ITEM_APPWIDGET: {
intappWidgetId = 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;
} caseAddAdapter.ITEM_LIVE_FOLDER: {
// Insert extra item to handle inserting folder
Bundle bundle = new Bundle(); ArrayList<String>shortcutNames = newArrayList<String>();
shortcutNames.add(res.getString(R.string.group_folder));
bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); ArrayList<ShortcutIconResource>shortcutIcons =
newArrayList<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;
} caseAddAdapter.ITEM_WALLPAPER: {
startWallpaper();
break;
}
}
} publicvoidonShow(DialogInterface dialog) {
mWaitingForResult = true;
}
} // pickShortcut()方法
privatevoidpickShortcut() {
Bundle bundle = new Bundle(); ArrayList<String>shortcutNames = newArrayList<String>();
shortcutNames.add(getString(R.string.group_applications));
bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); ArrayList<ShortcutIconResource>shortcutIcons = newArrayList<ShortcutIconResource>();
shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
R.drawable.ic_launcher_application));
bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
pickIntent.putExtras(bundle); startActivityForResult(pickIntent,REQUEST_PICK_SHORTCUT); // 启动了一个Activity,其实是一个快捷方式//列表,一个回调方法
} // 在onActivityResult()中 caseREQUEST_PICK_SHORTCUT:
processShortcut(data); // 回调此方法,data为Intent
break; voidprocessShortcut(Intent intent) {
// Handle case where user selected "Applications"
String applicationName = getResources().getString(R.string.group_applications);
String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); if (applicationName != null&&applicationName.equals(shortcutName)) {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
} else {
startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT); //这里还要启动一个Activity
}
} caseREQUEST_CREATE_SHORTCUT:
completeAddShortcut(data, mAddItemCellInfo);
break; /**
* Add a shortcut to the workspace.
*
* @param data The intent describing the shortcut.
* @paramcellInfo The position on screen where to create the shortcut.
*/
privatevoidcompleteAddShortcut(Intent data, CellLayout.CellInfocellInfo) {
cellInfo.screen = mWorkspace.getCurrentScreen();
if (!findSingleSlot(cellInfo)) return; finalShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false); if (!mRestoring) {
final View view = createShortcut(info);
mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
isWorkspaceLocked());
}
}

(8)      如何让你的应用支持创建Shortcut,即你的应用能够出现在快捷方式列表中。

//  单独创建一个Activity,例如ShortCut.java
// 配置此Activity
<activity
android:name=".ShortCut">
<intent-filter>
<actionandroid:name="android.intent.action.CREATE_SHORTCUT"/>
</intent-filter>
</activity>
// 以下是它的代码,这个startActivityForResultSafely(intent, //REQUEST_CREATE_SHORTCUT)方法会跳到这个Activity
publicclassShortCutextends Activity { @Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)) {
Intent returnIntent = new Intent();
returnIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "应用管理器");
returnIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this, ShowAppActivity.class));
/* returnIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); */
returnIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, R.drawable.icon);
setResult(RESULT_OK, returnIntent);
finish();
}
} }

(9)      如何在用户第一次启动应用后,提示用户是否要创建Shortcut:现在Manifest文件中添加一个权限(<permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"></permission>)只要向Launcher发送一个广播,数据为一个Intent。此Intent与上面代码中的returnIntent类似,只要这个Intent添加一个Action(

intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"))。然后sendBroadcast(intent)。在Launcher中有个InstallShortcutReceiver.java,接受这个广播。

Android Launcher 详解的更多相关文章

  1. Android——Android Bundle详解(转)

    Android Bundle详解 1 Bundle介绍 Bundle主要用于传递数据:它保存的数据,是以key-value(键值对)的形式存在的. 我们经常使用Bundle在Activity之间传递数 ...

  2. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  3. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  4. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

  5. Android 签名详解

    Android 签名详解 AndroidOPhoneAnt设计模式Eclipse  在Android 系统中,所有安装 到 系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程 ...

  6. Android编译系统详解(一)

    ++++++++++++++++++++++++++++++++++++++++++ 本文系本站原创,欢迎转载! 转载请注明出处: http://blog.csdn.net/mr_raptor/art ...

  7. Android布局详解之一:FrameLayout

      原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...

  8. 【整理修订】Android.mk详解

    Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 ...

  9. Android菜单详解(四)——使用上下文菜单ContextMenu

    之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...

随机推荐

  1. robot.libdocpkg package

    mplements the Libdoc tool. The command line entry point and programmatic interface for Libdoc are pr ...

  2. 完美解决Informix的中文乱码问题

    完美解决Informix的中文乱码问题  Informix是IBM旗下的一款数据库,要不是这个项目需要,估计这辈子我都不知道居然还有这么一款数据库.想来公司的项目遍布全国各地,各种部署环境各种应用场景 ...

  3. Flask+mongodb 实现简易个人博客

    最近学习完了<flask-web开发>,实现了一个简易的个人博客网站,由flask+mongodb+bootstrap做成, 这个软件是在阅读<Flask-Web开发>后写的一 ...

  4. 小例子(二)、winform窗体间的关系

    写一个关于winform窗体间的关系 1.登陆,思路:登陆后隐藏登陆窗体,关闭Form2时结束整个应用程序. //登陆窗体 private void button2_Click(object send ...

  5. (29)odoo的可用小图标

    * 系统的小图标都采用了 fontawesome    官网是 http://fontawesome.dashgame.com/    * 运用小图标    # 首先打开官网 http://fonta ...

  6. PHP生成word的三种方式

    摘要: 最近工作遇到关于生成word的问题 现在总结一下生成word的三种方法. btw:好像在博客园发表博客只要是标题带PHP的貌似点击量都不是很高(哥哥我标题还是带上PHP了),不知道为什么,估计 ...

  7. linux 新增挂载新硬盘

    1. 添加磁盘,查看磁盘状况 [root@db1 /]# fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sector ...

  8. HBase High Level Architecutre

  9. MongoDB常用操作一查询find方法db.collection_name.find()

    来:http://blog.csdn.net/wangli61289/article/details/40623097 https://docs.mongodb.org/manual/referenc ...

  10. JDBC 元数据 事务处理

    使用 JDBC 驱动程序处理元数据 Java 通过JDBC获得连接以后,得到一个Connection 对象,可以从这个对象获得有关数据库管理系统的各种信息,包括数据库中的各个表,表中的各个列,数据类型 ...