android l 中AppWidgetService的启动

本文详细解析了Android系统中AppWidgetService的启动过程,包括SystemServer如何调用SystemServiceManager启动AppWidgetService,以及AppWidgetService如何初始化并监听系统启动阶段。
摘要由CSDN通过智能技术生成

在android l中,系统服务的启动分为6个启动阶段,定义在SystemServer类中,大部分服务继承自SystemService类,通过重写onStart方法实现自身的初始化,同时可以重写onBootPhase方法来监听启动流程,以便在合适的时候执行相应动作,这些方法会由SystemServiceManager来调用。AppWidgetService亦是如此。

服务的启动只有一句,在在SystemServer的startOtherServices方法中

mSystemServiceManager.startService(APPWIDGET_SERVICE_CLASS);

具体分析其初始化

Created with Raphaël 2.1.2 SystemServer SystemServer SystemServiceManager SystemServiceManager startOtherServices startService("com.android.server.appwidget.AppWidgetService") startService(AppWidgetService.class)

在SystemServiceManager.startService中,主要是实例化AppWidgetService类以及回调它的onStart方法

    public <T extends SystemService> T startService(Class<T> serviceClass) {
        // 检查服务是否继承自SystemService
        final String name = serviceClass.getName();
        Slog.i(TAG, "Starting " + name);

        // Create the service.
        if (!SystemService.class.isAssignableFrom(serviceClass)) {
            throw new RuntimeException("Failed to create " + name
                    + ": service must extend " + SystemService.class.getName());
        }
        final T service;
        try {
            Constructor<T> constructor = serviceClass.getConstructor(Context.class);
            service = constructor.newInstance(mContext);
        } catch (InstantiationException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service could not be instantiated", ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service must have a public constructor with a Context argument", ex);
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service must have a public constructor with a Context argument", ex);
        } catch (InvocationTargetException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service constructor threw an exception", ex);
        }

        // 保存引用,方便在各个boot phase回调其方法
        mServices.add(service);

        // Start it.
        try {
            service.onStart();
        } catch (RuntimeException ex) {
            throw new RuntimeException("Failed to start service " + name
                    + ": onStart threw an exception", ex);
        }
        return service;
    }

AppWidgetService的实现很简单

public class AppWidgetService extends SystemService {
    private final AppWidgetServiceImpl mImpl;

    public AppWidgetService(Context context) {
        super(context);
        mImpl = new AppWidgetServiceImpl(context);
    }

    @Override
    public void onStart() {
        // 注册该服务为实名系统服务appWidget
        publishBinderService(Context.APPWIDGET_SERVICE, mImpl);
        AppWidgetBackupBridge.register(mImpl);
    }

    @Override
    public void onBootPhase(int phase) {
        if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
            mImpl.setSafeMode(isSafeMode());
        }
    }
}

其中onStart方法在前面已经调用,onBootPhase则在每个boot phase时由SystemServiceManager的startBootPhase方法调用。

接下来看一下AppWidgetServiceImpl的初始化

    AppWidgetServiceImpl(Context context) {
        mContext = context;
        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);
        mSaveStateHandler = BackgroundThread.getHandler();
        mCallbackHandler = new CallbackHandler(mContext.getMainLooper());
        mBackupRestoreController = new BackupRestoreController();
        mSecurityPolicy = new SecurityPolicy();
        computeMaximumWidgetBitmapMemory();
        registerBroadcastReceiver();
        registerOnCrossProfileProvidersChangedListener();
    }

主要是获取各种系统服务的引用,其中注册BroadcastReceiver用于监听包安装,系统配置改变,系统用户该表等事件。

可以看到在AppWidgetService的初始化过程中仅仅只是初始化一些引用,监听系统变化,并没有开始做任何的数据加载等操作。
数据加载操作应该会在首次使用AppWidgetService时调用,有必要看一下内部的几个重要的数据结构,它们用来描述AppWidget框架中Host和Provider的信息。
1. ProviderId:包含user和component信息
2. Provider:用于描述一个Provider
3. HostId:包含user,host id和package信息
4. Host:用于描述一个Host
5. Widget:描述一个显示的小插件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值