Android Launcher结构分析

来源:互联网 发布:重生之一叶而知秋书包 编辑:程序博客网 时间:2024/06/13 14:43

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

一、Launcher的定义及构成:

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

[html] view plain copy
  1. <activity  
  2.             Android:name="Launcher"  
  3.             android:launchMode="singleTask"  
  4.             android:clearTaskOnLaunch="true"  
  5.             android:stateNotNeeded="true"  
  6.             android:theme="@android:style/Theme.Wallpaper.NoTitleBar"  
  7.             android:screenOrientation="nosensor"  
  8.             android:windowSoftInputMode="stateUnspecified|adjustPan">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.HOME"/>  
  12.                 <category android:name="android.intent.category.DEFAULT" />  
  13.                 <category android:name="android.intent.category.MONKEY" />  
  14.             </intent-filter>  
  15.         </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的配置文件里面有这样一个广播接收者用于监听添加快捷方式,查看InstallShortcutReceiver的源码:

     

[html] view plain copy
  1. public class InstallShortcutReceiver extends BroadcastReceiver {  
  2.     private static final String ACTION_INSTALL_SHORTCUT =  
  3.             "com.android.launcher.action.INSTALL_SHORTCUT";  
  4.   
  5.     private final int[] mCoordinates = new int[2];  
  6.   
  7.     public void onReceive(Context context, Intent data) {  
  8.         if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) {  
  9.             return;  
  10.         }  
  11.         int screen = Launcher.getScreen();  
  12.         if (!installShortcut(context, data, screen)) {  
  13.             // The target screen is full, let's try the other screens  
  14.             for (int i = 0; i < Launcher.SCREEN_COUNT; i++) {  
  15.                 if (i != screen && installShortcut(context, data, i)) break;  
  16.             }  
  17.         }  
  18.     }  
  19. }  

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




2、LiveFolder

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

[java] view plain copy
  1. // Insert extra item to handle inserting folder  
  2.                  Bundle bundle = new Bundle();  
  3.   
  4.                  ArrayList<String> shortcutNames = new ArrayList<String>();  
  5.                  shortcutNames.add(res.getString(R.string.group_folder));  
  6.                  bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);  
  7.   
  8.                  ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();  
  9.                  shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, R.drawable.ic_launcher_folder));  
  10.                  bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);  
  11.   
  12.                  Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);  
  13.                  pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));  
  14.                  pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_live_folder));  
  15.                  pickIntent.putExtras(bundle);  
  16.   
  17.                  startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);  

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


[java] view plain copy
  1.     void addLiveFolder(Intent intent) { // RESULT_PICK_LIVE_FOLDER  
  2.         // Handle case where user selected "Folder"  
  3.         String folderName = getResources().getString(R.string.group_folder);  
  4.         String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);  
  5.   
  6.         if (folderName != null && folderName.equals(shortcutName)) {  
  7.             addFolder(!mDesktopLocked);  
  8.         } else {  
  9.             startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);  
  10.         }  
  11.     }  
  12.   
  13. 完成添加  
  14.   
  15.     private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo,  
  16.             boolean insertAtFirst) { // REQUEST_CREATE_LIVE_FOLDER  
  17.         cellInfo.screen = mWorkspace.getCurrentScreen();  
  18.         if (!findSingleSlot(cellInfo)) return;  
  19.   
  20.         final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);  
  21.   
  22.         if (!mRestoring) {  
  23.             sModel.addDesktopItem(info);  
  24.   
  25.             final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,  
  26.                 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);  
  27.             mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 11, insertAtFirst);  
  28.         } else if (sModel.isDesktopLoaded()) {  
  29.             sModel.addDesktopItem(info);  
  30.         }  
  31.     }  


3、AppWidget:AppWidgetProvider用来在HOME页面显示插件

实现步骤:

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

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

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


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

[java] view plain copy
  1. public class MainActivity extends AppWidgetProvider {  
  2.   
  3.     @Override  
  4.     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
  5.         super.onUpdate(context, appWidgetManager, appWidgetIds);  
  6.         Timer timer = new Timer();  
  7.         timer.scheduleAtFixedRate(new LYTimeTask(context, appWidgetManager), 150000);  
  8.     }  
  9.   
  10.     private class LYTimeTask extends TimerTask {  
  11.         RemoteViews remoteViews;  
  12.         AppWidgetManager appWidgetManager;  
  13.         ComponentName widget;  
  14.   
  15.         @Override  
  16.         public void run() {  
  17.             Date date = new Date();  
  18.             Calendar calendar = new GregorianCalendar(20130724);  
  19.             long days = (calendar.getTimeInMillis() - date.getTime()) / 1000 / 86400;  
  20.             remoteViews.setTextViewText(R.id.worldcup, "还剩下" + days + "天");  
  21.             appWidgetManager.updateAppWidget(widget, remoteViews);  
  22.         }  
  23.   
  24.         public LYTimeTask(Context context, AppWidgetManager appWidgetManger) {  
  25.             super();  
  26.             this.appWidgetManager = appWidgetManger;  
  27.             remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);  
  28.             widget = new ComponentName(context, MainActivity.class);  
  29.         }  
  30.     };  
  31. }  

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

          
[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.   android:layout_width="match_parent"  
  3.   android:layout_height="match_parent"  
  4.   android:background="@drawable/worldcup"  
  5.   android:orientation="vertical" >  
  6.   <TextView  
  7.     android:id="@+id/babybirthday"  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="wrap_content"  
  10.     android:text="@string/message"  
  11.     android:textSize="12px"  
  12.     android:textColor="#ff0000" />  
  13. </LinearLayout>  

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

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

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

[html] view plain copy
  1. <application  
  2.   android:allowBackup="true"  
  3.   android:icon="@drawable/ic_launcher"  
  4.   android:label="@string/app_name"  
  5.   android:theme="@style/AppTheme" >  
  6.   <receiver  
  7.     android:name=".MainActivity"  
  8.     android:label="@string/app_name" >  
  9.     <intent-filter>   
  10.       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /><!--广播接收过滤器-->  
  11.     </intent-filter>  
  12.   
  13.     <meta-data  
  14.       android:name="android.appwidget.provider"  
  15.       android:resource="@xml/AppWigdetProvider_Provider" /><!--AppWidgetProvider引用的Provider文件-->  
  16.   </receiver>  
  17. </application>  

运行程序:进入WIDGETS页面,可将Widget添加到HOME页

在AppWidgetProvider类中,还有其它相关的方法

[java] view plain copy
  1. public class WidgetProvider extends AppWidgetProvider {  
  2.   
  3.     // 每接收一次广播消息就调用一次,使用频繁  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         super.onReceive(context, intent);  
  6.     }  
  7.   
  8.     // 每次更新都调用一次该方法,使用频繁  
  9.     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
  10.         super.onUpdate(context, appWidgetManager, appWidgetIds);  
  11.     }  
  12.    
  13.     // 每删除一个就调用一次  
  14.     public void onDeleted(Context context, int[] appWidgetIds) {  
  15.         super.onDeleted(context, appWidgetIds);  
  16.     }  
  17.    
  18.     // 当该Widget第一次添加到桌面是调用该方法,可添加多次但只第一次调用  
  19.     public void onEnabled(Context context) {  
  20.         super.onEnabled(context);  
  21.     }  
  22.    
  23.     // 当最后一个该Widget删除是调用该方法,注意是最后一个  
  24.     public void onDisabled(Context context) {  
  25.         super.onDisabled(context);  
  26.     }  
  27. }  

 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