AppWidgetService 启动
在SystemServer的main()方法中,调用SystemServer.run()
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
在run方法中,初始化系统上下文,启动三个服务,
-
startBootstrapServices()
该方法所创建的服务:ActivityManagerService, PowerManagerService, LightsService, DisplayManagerService, PackageManagerService, UserManagerService, sensor服务. -
startCoreServices()
启动服务BatteryService,UsageStatsService,WebViewUpdateService。 -
startOtherServices()
启动其他相关服务
在其他服务中启动AppWidgetService
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_APP_WIDGETS)
|| context.getResources().getBoolean(R.bool.config_enableAppWidgetService)) {
t.traceBegin("StartAppWidgetService");
mSystemServiceManager.startService(APPWIDGET_SERVICE_CLASS);
t.traceEnd();
}
最终调用service.start()方法,实现AppWidgetServiceImpl的onStart方法,
public void startService(@NonNull final SystemService service) {
// Register it.
mServices.add(service);
// Start it.
long time = SystemClock.elapsedRealtime();
try {
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + service.getClass().getName()
+ ": onStart threw an exception", ex);
}
warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
}
AppWidgetServiceImlp 中初始化相关管理器,
public void onStart() {
mPackageManager = AppGlobals.getPackageManager();
mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
mKeyguardManager = (KeyguardManager) mContext.getSystemService(KEYGUARD_SERVICE);
mDevicePolicyManagerInternal = LocalServices.getService(DevicePolicyManagerInternal.class);
mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
mSaveStateHandler = BackgroundThread.getHandler();
mCallbackHandler = new CallbackHandler(mContext.getMainLooper());
mBackupRestoreController = new BackupRestoreController();
mSecurityPolicy = new SecurityPolicy();
// 将内存使用限制为显示器大小的1.5倍
computeMaximumWidgetBitmapMemory();
// 注册有关软件包安装等的广播,以便我们可以
// 更新提供者列表。
registerBroadcastReceiver();
// 添加是可选组件的设备策略改变监听
registerOnCrossProfileProvidersChangedListener();
LocalServices.addService(AppWidgetManagerInternal.class, new AppWidgetManagerLocal());
}
Launcher
在launcher主界面
@Override
255 protected void onCreate(Bundle savedInstanceState) {
272 super.onCreate(savedInstanceState);
...
289 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
290 // mAppWidgetHost是launcher承载AppWidgetView的宿主
291 mAppWidgetHost = new LauncherAppWidgetHost(this);
292 mAppWidgetHost.startListening();