14、AppWidget及Launcher

來源:互聯網
上載者:User

標籤:android   style   blog   color   io   os   java   ar   strong   

一、Launcher的簡單研究

1 什麼是Launcher

Android系統啟動後載入的第一個程式 。 這個程式是其他應用程式的入口 。

Launcher構成:

HomeScreen : (Workspace + HotSeats)  , Shortcut,LiveFolder , AppWidget , WallPaper .

AllAppList (用GridView)

2 針對Launcher的開發類型

1)開發自己的Launcher

2)在Wordspace做文章

添加Shortcut ;

添加LiveFolder ;

更改背景 ;

添加AppWidget ;

 3 分析Launcher.java的onCreate()

 1 @Override 2     protected void onCreate(Bundle savedInstanceState) { 3         super.onCreate(savedInstanceState); 4      //下面一段是介面初始化工作 5         LauncherApplication app = ((LauncherApplication)getApplication()); 6         mModel = app.setLauncher(this); 7         mIconCache = app.getIconCache(); 8         //得到拖動組件執行個體 9      mDragController = new DragController(this);10         //得到Inflater執行個體11      mInflater = getLayoutInflater();12 13      //得到AppWidget管理執行個體 : AppWidgetManager , AppWidgetHost , AppWidgetHostView三個類的關係14         mAppWidgetManager = AppWidgetManager.getInstance(this);15         mAppWidgetHost = new LauncherAppWidgetHost(16           this, APPWIDGET_HOST_ID);17         //Host啟動監聽,監聽LauncherProvider中的資料改變18      mAppWidgetHost.startListening();19 20         if (PROFILE_STARTUP) {21             android.os.Debug.startMethodTracing(22                     Environment.getExternalStorageDirectory() + "/launcher");23         }24 25         loadHotseats();26      //檢查本地化語言改變27         checkForLocaleChange();28      //設定案頭尺寸29         setWallpaperDimension();30 31      //載入內容顯示32         setContentView(R.layout.launcher);33         //設定動態每個案頭視圖(左右滑動的那個)34      setupViews();35      //註冊內容觀察36         registerContentObservers();37      //鎖定應用程式38         lockAllApps();39 40         mSavedState = savedInstanceState;41         restoreState(mSavedState);42 43         if (PROFILE_STARTUP) {44             android.os.Debug.stopMethodTracing();45         }46 47         if (!mRestoring) {48             mModel.startLoader(this, true);49         }50 51         // For handling default keys52         mDefaultKeySsb = new SpannableStringBuilder();53         Selection.setSelection(mDefaultKeySsb, 0);54 55         IntentFilter filter = new IntentFilter(56           Intent.ACTION_CLOSE_SYSTEM_DIALOGS);57         registerReceiver(mCloseSystemDialogsReceiver, filter);58     }

4 HomeScreen可以添加的四大件

Shortcut

LiveFolder

AppWidget

WallPaper

5 分析機制-SequenceDiagram

 

6 Launcher的介面分析

 

7 AppWidget監聽機制

二、Shortcut&LiveFolder

1 什麼是Shortcut?

 

2 實現Shortcut步驟

1) 建立activity 

2) manifest裡面 註冊並且加上intent-filter,name為

  android.intent.action.CREATE_SHORTCUT

3) acitivity ,建立一個Intent 執行個體 , 設定Extra_***屬性,然後返回。

 

3 添加Shortcut到案頭

 

4 什麼是LiveFolder

1) 建立activity 

2) manifest裡面 註冊並且加上intent-filter,name為

  android.intent.action.CREATE_LIVEFOLDER

3) acitivity ,建立一個Intent 執行個體 , 設定Extra_***屬性 和 Data屬性,

然後返回.

 

5 實現LiveFolder步驟

 

三、AppWidget

3.1 什麼是AppWidget

AppWidget如果有你設計 , 你覺得他和Shortcut&Livefolder有什麼區別

1)介面顯示複雜,必須要用到VIewGroup(Layout)。

2)事件不再單一。

3)進程間通訊。不斷通訊和重新整理。

【設計思路】

Serivce&AppWidgetManager    Binder

序列化的複雜介面傳輸

為了重新整理,Broadcast , BroadcastReceiver

3.2 AppWidget主要架構及類介紹

1> AppWidgetHost    ------ 介面相關

2> AppWidgetHostView    ------ 介面相關

3> AppWidgetManager

4> AppWidgetProvider

 

5> AppWidgetProviderInfo

5.1 Meta-Data介紹

 

5.2 MinWidth,MinHeight

Widget 是按照儲存格劃分的 , 每個儲存格大小是74dp , 每個widget還需要減去兩邊個1dp , 即你的真實的寬高演算法為:(number of cells * 74) - 2

另外,我們預設約定整個workspace :

平板共有 8*7 個儲存格 

 

自動補齊機制

【AppWidget對象剖析】

 

5.3 updatePeriodMillis

更新頻率

一般不推薦30分鐘以上時間更新一次的頻率(沒有意義),但也不推薦小於5分鐘的更新頻率(耗電)

 

5.4 initialLayout

5.5 configure

設定添加AppWidget時候啟動的設定Activity(過度Activity)

 

5.6 previewImage

設定AppWidget添加後的預覽圖

 

6> RemoteViews

    RemoteViews的作用

    RemoteViews可以用如下的layout

    FrameLayout

    LinearLayout

    RelativeLayout

    以及如下的View元素:

    AnalogClock

    Button

    Chronometer

    ImageButton

    ImageView

    ProgressBar

    TextView

    ViewFlipper

    ListView

    GridView

    StackView

    AdapterViewFlipper

 

3.3 AppWidget兩個過程

【Initialize】

【Update】

1)launcher 啟動host listening

2)service send broadcast

3)myapp 接收到 , onUpdate

4,)回傳了RemoteViews -> Service

5)Service 改變Host ,updateAppWidget

6)Launcher監聽到了,呈現

 

3.4 實驗 【詳解AppWidget建立及互動機制】

1> AppWidgetHelloworld

【步驟】

   1)建立一個AppWidgetProvider子類;

   2)Manifest中註冊子類,並且註冊AppWidgetProviderInfo設定檔;

   3)完成AppWidgetProviderInfo設定檔;

   4)完成Layout檔案;

   5)實現AppWidgetProvider onUpdate 來重新整理顯示 。

2> 給當前AppWidget加入點擊事件支援並且更改view狀態。

3> 給當前AppWidget加入ConfigrationActivity支援。

4> 我在Widget要去觸發我的App裡面的事件?  SendMessage

 

3.5 其實官方提供了一個很好的案例: Wiktionary & StackView Widget.

 

3.6 總結Widget重點痛點理解。

Widget實際上是?   看一下Launcher.

Widget介面大小.

meta-data name必須是android.appwidget.provider.

 

四、如何來設計我們自己的Launcher

1 瞭解系統啟動過程

2 瞭解系統如何去識別和啟動Launcher的

<intent-filter>

  <action android:name="android.intent.action.MAIN" />

  <category android:name="android.intent.action.HOME"/>  

  <category android:name="android.intent.action.DEFAULT"/>       

</intent-filter>

3 設計架構介面

3.1 HomeScreen

  Workspace

>Shortcut

>Livefolder

>Wallpaper -------- Graphic & Animation ,OpenGL ES。

>AppWidget ------- 熟悉AppWidget相關的Framework類。

3.2 Hotseats

AllApplist -------- 通過PackageManager載入我們的app。

4 其它

皮膚  縱橫螢幕  多國語言  ......

 

14、AppWidget及Launcher

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.