Launcher是系统启动后第一个启动的程序,是其它应用程序的入口,也就是我们的手机程序的桌面程序;
一、Launcher的定义及构成:
<1>通过查看官方提供的Launcher源码可以知道其实Launcher也是一个Activity,不过它的intent-fliter有点特殊;
Launcher的intent-filter中,action为intent.action.MAIN,表示该Activity是程序的主入口,但是它的category是category.HOME,和一般的app不一样,category.HOME则标识了这个Activity是一个Launcher,其实就是对应的按下HOME键所跳转到的Activity,也就是我们的桌面;
下面我们再来看一下一个普通的App的程序主入口Activity的配置:
表示该类是程序主入口;
category.LAUNCHER表示该Activity在Launcher上可见,所以这一个Activity会被添加到Launcher;
<2>Launcher构成:
HomeScreen(WorkSpace+HotSeats), Shortcut(快捷方式), LiveFolder(文件夹), AppWidget(窗口小部件), WallPaper(壁纸); AllAppList:
下面我们就来分别研究探讨这四个元素
1、Shortcut
在Launcher的配置文件里面有这样一个广播接收者用于监听添加快捷方式,查看InstallShortcutReceiver的源码:
public class InstallShortcutReceiver extends BroadcastReceiver {
private static final String ACTION_INSTALL_SHORTCUT =
"com.android.launcher.action.INSTALL_SHORTCUT";
private final int[] mCoordinates = new int[2];
public void onReceive(Context context, Intent data) {
if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) {
return;
}
int screen = Launcher.getScreen();
if (!installShortcut(context, data, screen)) {
// The target screen is full, let's try the other screens
for (int i = 0; i < Launcher.SCREEN_COUNT; i++) {
if (i != screen && installShortcut(context, data, i)) break;
}
}
}
}
2、LiveFolder
在Launcher.java文件中,找到添加LiveFolder的入口
// Insert extra item to handle inserting folder
Bundle bundle = new Bundle();
ArrayList shortcutNames = new ArrayList();
shortcutNames.add(res.getString(R.string.group_folder));
bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
ArrayList shortcutIcons = new ArrayList();
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);
void addLiveFolder(Intent intent) { // RESULT_PICK_LIVE_FOLDER
// Handle case where user selected "Folder"
String folderName = getResources().getString(R.string.group_folder);
String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
if (folderName != null && folderName.equals(shortcutName)) {
addFolder(!mDesktopLocked);
} else {
startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
}
}
完成添加
private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,
boolean insertAtFirst) { // REQUEST_CREATE_LIVE_FOLDER
cellInfo.screen = mWorkspace.getCurrentScreen();
if (!findSingleSlot(cellInfo)) return;
final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
if (!mRestoring) {
sModel.addDesktopItem(info);
final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
(ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst);
} else if (sModel.isDesktopLoaded()) {
sModel.addDesktopItem(info);
}
}
3、AppWidget:AppWidgetProvider用来在HOME页面显示插件
实现步骤:
>>1、在res/xml/文件夹下创建AppWigdetProvider_Provider.xml文件
android:minHeight="50dip"
android:minWidth="50dip"
android:updatePeriodMillis="5000" >
public class MainActivity extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new LYTimeTask(context, appWidgetManager), 1, 50000);
}
private class LYTimeTask extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName widget;
@Override
public void run() {
Date date = new Date();
Calendar calendar = new GregorianCalendar(2013, 07, 24);
long days = (calendar.getTimeInMillis() - date.getTime()) / 1000 / 86400;
remoteViews.setTextViewText(R.id.worldcup, "还剩下" + days + "天");
appWidgetManager.updateAppWidget(widget, remoteViews);
}
public LYTimeTask(Context context, AppWidgetManager appWidgetManger) {
super();
this.appWidgetManager = appWidgetManger;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
widget = new ComponentName(context, MainActivity.class);
}
};
}
>>4、修改程序自动生成的清单文件。在AndroidManifest.xml中,声明上述的AppWidgetProvider的子类是一个Receiver,并且:
(1)、该Receiver的intent-filter的Action必须包含“android.appwidget.action.APPWIDGET_UPDATE”;
(2)、该Receiver的meta-data为“android.appwidget.provider”,并用一个xml文件来描述布局属性。
运行程序:进入WIDGETS页面,可将Widget添加到HOME页
在AppWidgetProvider类中,还有其它相关的方法
public class WidgetProvider extends AppWidgetProvider {
// 每接收一次广播消息就调用一次,使用频繁
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
// 每次更新都调用一次该方法,使用频繁
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
// 每删除一个就调用一次
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
// 当该Widget第一次添加到桌面是调用该方法,可添加多次但只第一次调用
public void onEnabled(Context context) {
super.onEnabled(context);
}
// 当最后一个该Widget删除是调用该方法,注意是最后一个
public void onDisabled(Context context) {
super.onDisabled(context);
}
}
AppWidgetProvider definition
meta-data resource to provider.xml
provider xml to layout.xml
create AppWidgetInfo transact();
Launcher和AppWidget交互流程如下: