網(wǎng)站首頁 編程語言 正文
一、AppWidget開機啟動加載
Android系統(tǒng)啟動,SystemServer創(chuàng)建AppWidgetService,并調(diào)用AppWidgetServiceImpl.onStart()方法,初始化相關(guān)管理器,注冊四個廣播接收器:
第一、注冊有關(guān)軟件包安裝、刪除、改變的廣播,以便我們可以更新provider列表。;
// Register for broadcasts about package install, etc., so we can // update the provider list. IntentFilter packageFilter = new IntentFilter(); packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED); packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); packageFilter.addDataScheme("package"); mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, packageFilter, null, null);
第二、當(dāng)鎖定狀態(tài)發(fā)生變化時會播放一些事件:
IntentFilter offModeFilter = new IntentFilter(); offModeFilter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); offModeFilter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, offModeFilter, null, null);
第三、暫停或解除暫停應(yīng)用廣播;
IntentFilter offModeFilter = new IntentFilter(); offModeFilter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); offModeFilter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, offModeFilter, null, null);
第四、注冊sdcard的安裝與卸載的廣播。
// Register for events related to sdcard installation. IntentFilter sdFilter = new IntentFilter(); sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, sdFilter, null, null);
二、Launcher3啟動添加
Launcher啟動onCreate()方法初始化mAppWidgetManager, mAppWidgetHost對象,AppWidgetHost是launcher承載AppWidgetView的宿主
mAppWidgetHost.startListening,通過
public void onCreate() { ... // 13 //得到AppWidget管理實例 : AppWidgetManager , AppWidgetHost , AppWidgetHostView三個類的關(guān)系 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this); mAppWidgetHost = new LauncherAppWidgetHost(this); // Host啟動監(jiān)聽,監(jiān)聽LauncherProvider中的數(shù)據(jù)改變 mAppWidgetHost.startListening(); ... }
1、初始化AppWidgetHostd對象,創(chuàng)建用于回調(diào)的Callbacks服務(wù)類IAppWidgetHost.Stub, 綁定服務(wù)bindService,得到IAppWidgetService對象,進行l(wèi)auncher和AppWidgetService之間的調(diào)用
public AppWidgetHost(Context context, int hostId, InteractionHandler handler, Looper looper) { mContextOpPackageName = context.getOpPackageName(); mHostId = hostId; mInteractionHandler = handler; mHandler = new UpdateHandler(looper); mCallbacks = new Callbacks(mHandler); mDisplayMetrics = context.getResources().getDisplayMetrics(); bindService(context); }
2、在startListening 方法中 ,通過IAppWidgetService.startListening 方法解析Launcher中的AppWidget信息保存到系統(tǒng)服務(wù)成員變量中, 3、當(dāng)添加AppWidget時,首頁返回到Launcher中的onActivityResult中,在handleActivityResult中
addAppWidgetImpl( appWidgetId, requestArgs, null, requestArgs.getWidgetHandler(), ON_ACTIVITY_RESULT_ANIMATION_DELAY);
創(chuàng)建添加小部件意圖,之后返回到onActivityForResult,調(diào)用completeAddAppWidget,通過IAppWidgetService.getAppWidgetInfo,獲取AppWidgetProviderInfo,保存到本地數(shù)據(jù)庫中addItemToDatabase(),
@Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo, 1070 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) { 1071 1072 if (appWidgetInfo == null) { 1073 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId); 1074 } 1075 1076 LauncherAppWidgetInfo launcherInfo; 1077 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider); 1078 launcherInfo.spanX = itemInfo.spanX; 1079 launcherInfo.spanY = itemInfo.spanY; 1080 launcherInfo.minSpanX = itemInfo.minSpanX; 1081 launcherInfo.minSpanY = itemInfo.minSpanY; 1082 launcherInfo.user = appWidgetInfo.getProfile(); 1083 1084 getModelWriter().addItemToDatabase(launcherInfo, 1085 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY); 1086 1087 if (hostView == null) { 1088 // Perform actual inflation because we're live 1089 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); 1090 } 1091 hostView.setVisibility(View.VISIBLE); 1092 prepareAppWidget(hostView, launcherInfo); 1093 mWorkspace.addInScreen(hostView, launcherInfo); 1094 } 1095 1096 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) { 1097 hostView.setTag(item); // 計算AppWidget的顯示大小位置 1098 item.onBindAppWidget(this, hostView); 1099 hostView.setFocusable(true); 1100 hostView.setOnFocusChangeListener(mFocusHandler); 1101 }
并創(chuàng)建AppWidgetHostView 對象,mAppWidgetHost.createView,返回RemoteView對象,IAppWidgetService。getAppWidgetViews()
public final AppWidgetHostView createView(Context context, int appWidgetId, AppWidgetProviderInfo appWidget) { if (sService == null) { return null; } AppWidgetHostView view = onCreateView(context, appWidgetId, appWidget); view.setInteractionHandler(mInteractionHandler); view.setAppWidget(appWidgetId, appWidget); synchronized (mViews) { mViews.put(appWidgetId, view); } RemoteViews views; try { views = sService.getAppWidgetViews(mContextOpPackageName, appWidgetId); } catch (RemoteException e) { throw new RuntimeException("system server dead?", e); } view.updateAppWidget(views); return view; }
調(diào)用AppWidgetHostView.updateAppWidget(views);更新View到launcher界面上mWorkspace.addInScreen(hostView, launcherInfo);
當(dāng)AppWidgetProvider獲得更新的廣播,并執(zhí)行onUpdate(),onUpdate()中創(chuàng)建了RemoteViews并通過AppWidgetManager.updateAppWidget()更新到AppWidgetService之后,AppWidgetService會通過注冊的IAppWidgetHost的回調(diào),執(zhí)行AppWidgetHost的更新。
總結(jié)
原文鏈接:https://blog.csdn.net/u012743062/article/details/122340099
相關(guān)推薦
- 2022-11-30 jQuery中隱藏元素的hide方法及說明_jquery
- 2022-06-15 C#實現(xiàn)冒泡排序和插入排序算法_C#教程
- 2022-05-09 輕量級ORM框架Dapper應(yīng)用之實現(xiàn)DTO_實用技巧
- 2023-02-07 Go動態(tài)調(diào)用函數(shù)的實例教程_Golang
- 2023-01-23 C++中引用處理的基本方法_C 語言
- 2023-04-26 Python?Flask的request對象使用詳解_python
- 2022-08-03 Flutter實現(xiàn)切換應(yīng)用時隱藏應(yīng)用預(yù)覽_Android
- 2022-04-06 python中matplotlib的顏色以及形狀實例詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支