Launcher是系统启动后第一个启动的程序,是其它应用程序的入口,也就是我们的手机程序的桌面程序;

一、Launcher的定义及构成:

<1>通过查看官方提供的Launcher源码可以知道其实Launcher也是一个Activity,不过它的intent-fliter有点特殊;

        <activity
android:name="Launcher"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:theme="@android:style/Theme.Wallpaper.NoTitleBar"
android:screenOrientation="nosensor"
android:windowSoftInputMode="stateUnspecified|adjustPan">
<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>

Launcher的intent-filter中,action为intent.action.MAIN,表示该Activity是程序的主入口,但是它的category是category.HOME,和一般的app不一样,category.HOME则标识了这个Activity是一个Launcher,其实就是对应的按下HOME键所跳转到的Activity,也就是我们的桌面;

下面我们再来看一下一个普通的App的程序主入口Activity的配置:

<action android:name="android.intent.action.MAIN" /> 表示该类是程序主入口;
<category android:name="android.intent.category.LAUNCHER" />

category.LAUNCHER表示该Activity在Launcher上可见,所以这一个Activity会被添加到Launcher;

<2>Launcher构成:
HomeScreen(WorkSpace+HotSeats),Shortcut(快捷方式),LiveFolder(文件夹),AppWidget(窗口小部件),WallPaper(壁纸);
AllAppList:

下面我们就来分别研究探讨这四个元素

1、Shortcut
在Launcher的配置文件里面有这样一个广播接收者用于监听添加快捷方式

        <receiver
android:name=".InstallShortcutReceiver"
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>
</receiver>

查看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;
}
}
}
}

从上面的核心代码可以看出,在BroadcastReceiver接收到INSTALL_SHORTCUT广播之后,会尝试在当前屏添加快捷方式,如果当前屏满了,则会尝试将快捷方式添加到其它屏;因此,当我们想主动添加一个快捷方式到屏幕的时候,其实就很简单了,发送一个广播,并且设置它的Action为INSTALL_SHORTCUT即可;但是,需要注意的是如果仅仅设置Action是不够的,因为仅仅这样做会重复添加快捷方式,如果想不重复添加,还得做一些设置,详情参考这里

2、LiveFolder

在Launcher.java文件中,找到添加LiveFolder的入口

                    // 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);

当我们长按桌面之后,选择添加文件夹,则会执行上面这段代码,在这里会先创建好一个空文件夹;

    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. 为AppWidget提供一个元布局文件AppWigdetProvider_Provider.xml,用来显示Widget的界面。
  2. 创建一个类继承自AppWidgetProvider,并覆写里面的相关的方法并且注册到配置文件。
  3. 为WidgetProvider创建一个引用的布局文件。

>>1、在res/xml/文件夹下创建AppWigdetProvider_Provider.xml文件

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  android:initialLayout="@layout/main" <!-- android:initialLayout 设置引用的布局文件 -->
  android:minHeight="50dip"
  android:minWidth="50dip"
  android:updatePeriodMillis="5000" > <!-- 设置更新时间,单位为毫秒 -->
</appwidget-provider>

>>2、修改MainActivity继承自AppWidgetProvider并覆写里面的一些方法,实际上AppWidgetProvider就是一个BroadcastReceiver;

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);
}
};
}

>>3、为Widget创建一个显示用的布局文件:main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/worldcup"
  android:orientation="vertical" >
  <TextView
    android:id="@+id/babybirthday"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/message"
    android:textSize="12px"
    android:textColor="#ff0000" />
</LinearLayout>

>>4、修改程序自动生成的清单文件。在AndroidManifest.xml中,声明上述的AppWidgetProvider的子类是一个Receiver,并且:

(1)、该Receiver的intent-filter的Action必须包含“android.appwidget.action.APPWIDGET_UPDATE”;

(2)、该Receiver的meta-data为“android.appwidget.provider”,并用一个xml文件来描述布局属性。

<application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >
  <receiver
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /><!--广播接收过滤器-->
    </intent-filter>     <meta-data
      android:name="android.appwidget.provider"
      android:resource="@xml/AppWigdetProvider_Provider" /><!--AppWidgetProvider引用的Provider文件-->
  </receiver>
</application>

运行程序:进入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);
}
}

AppWidget本质上是一个AppWidgetHostView;

AppWidgetProvider definition
meta-data resource to provider.xml
provider xml to layout.xml
create AppWidgetInfo transact();

Launcher和AppWidget交互流程如下:

  1. Launcher 启动,开始监听
  2. Service send a broadcast
  3. myApp 接收到广播,执行onUpdate方法
  4. onUpdate方法回传RemoteViews给Service
  5. Service改变Host,updateAppWidget
  6. Launcher监听到,更新Appwidget

Android Launcher 研究学习的更多相关文章

  1. 【转】Android Launcher研究 (一)

    这份源码是基于2.1的launcher2,以后版本虽有变化,但大概的原理一直还是保留了. 一.主要文件和类  1.Launcher.java:launcher中主要的activity. 2.DragL ...

  2. android 1.6 launcher研究之自定义ViewGroup (转 2011.06.03(二)——— android 1.6 launcher研究之自定义ViewGroup )

    2011.06.03(2)——— android 1.6 launcher研究之自定义ViewGroup2011.06.03(2)——— android 1.6 launcher研究之自定义ViewG ...

  3. 【转】手把手教你读取Android版微信和手Q的聊天记录(仅作技术研究学习)

    1.引言 特别说明:本文内容仅用于即时通讯技术研究和学习之用,请勿用于非法用途.如本文内容有不妥之处,请联系JackJiang进行处理!   我司有关部门为了获取黑产群的动态,有同事潜伏在大量的黑产群 ...

  4. 手把手教你读取Android版微信和手Q的聊天记录(仅作技术研究学习)

    1.引言 特别说明:本文内容仅用于即时通讯技术研究和学习之用,请勿用于非法用途.如本文内容有不妥之处,请联系JackJiang进行处理!   我司有关部门为了获取黑产群的动态,有同事潜伏在大量的黑产群 ...

  5. Android Launcher 3 简单分析

    最近在学习Android Launcher的相关知识,在github上找到可以在Android studio上编译的Launcher 3代码,地址:https://github.com/rydanli ...

  6. Android安全研究经验谈

    安全研究做什么 从攻击角度举例,可以是:对某个模块进行漏洞挖掘的方法,对某个漏洞进行利用的技术,通过逆向工程破解程序.解密数据,对系统或应用进行感染.劫持等破坏安全性的攻击技术等. 而防御上则是:查杀 ...

  7. Android Launcher分析和修改13——实现Launcher编辑模式(1) 壁纸更换

    已经很久没更新Launcher系列文章,今天不分析源码,讲讲如何在Launcher里面添加桌面设置的功能.目前很多第三方Launcher或者定制Rom都有简单易用的桌面设置功能.例如小米MIUI的La ...

  8. 基于python的opcode优化和模块按需加载机制研究(学习与个人思路)(原创)

    基于python的opcode优化和模块按需加载机制研究(学习与思考) 姓名:XXX 学校信息:XXX 主用编程语言:python3.5 个人技术博客:http://www.cnblogs.com/M ...

  9. Android之Wifi学习(1)

    在Android中对Wifi操作,android本身提供了一些实用的包.在android.net.wifi包以下.简介一下: 大致能够分为四个基本的类ScanResult,wifiConfigurat ...

随机推荐

  1. [自娱自乐] 2、超声波测距模块DIY笔记(二)

    前言 上一节我们已经大致浏览下目前销售的超声波测距模块同时设计了自己的分析电路,这次由于我买的电子元件都到了,所以就动手实验了下!至写该笔记时已经设计出超声波接收模块和超声波发射模块,同时存在超声波发 ...

  2. [Java Web] 5、JSP (1) 注释 & Scriptlet

    >_<" 在JSP中支持两种注释的语法操作,一种是显式注释,这种注释客户端是允许看见的,另外一种是隐式注释,此种注释客户端是无法看见的. 显式注释语法: <!-- 注释内容 ...

  3. Linux:Linux 重要人物

    1.Ken Thompson:C 语言之父和 UNIX 之父 2.Dennis Ritchie:C 语言之父和 UNIX 之父 3.Stallman:著名黑客,GNU 创始人,开发了 Emacs.gc ...

  4. JAVA学习中Swing概述中的JFrame学习

    package com.swing; import java.awt.Color;import java.awt.Container;import java.awt.event.WindowAdapt ...

  5. maven pom.xml报错

    再在项目上强制update一下就可以了 如下: 此外使用maven时用默认的仓库速度会过慢 下载很小的jar包都需要很久 推介使用oschina的源 使用在这里:

  6. Unity3D编程学习分享

    学习地址:http://www.ixueyun.com/lessons/detail-lessonId-692.html 一.课程概述: 以前大部分3D游戏出现在pc和ps.XBox等专业游戏主机上, ...

  7. fine-uploader 5.11.8 (最新版) 使用感受

    以前测试和使用过 3.X 因为够用,所以一直没升级,今天闲来无聊测试了一下最新版.和3.X比性能上好了很多,但不支持了移动设备的多文件上传. 关于3.X版 可以看这里:http://www.cnblo ...

  8. YAFFS2文件系统分析(转)

    http://blog.chinaunix.net/uid-25314474-id-343665.html 1.前言略. 2.yaffs 文件系统简介按理说这里应该出现一些诸如“yaffs 是一种适合 ...

  9. 是时候放弃sublime了

    今天下午在忍无可忍之下终于卸载了sublime,最为一个在gui下最顺手的编辑器,放弃是需要非常充足的理由的. 放弃sublime无非是因为以下几点原因: 收费.我用的是未注册版的sublime,保存 ...

  10. 11G RAC 中 OCR 及Voting Disk 相关操作

    一.启动oracle clusterware先决条件:Oracle High Availability Services daemon(OHASD)运行在所有集群节点上1.启动整个Oracle Clu ...