亲宝软件园·资讯

展开

android Launcher AppWidget

空间维度 人气:0

一、AppWidget开机启动加载

Android系统启动,SystemServer创建AppWidgetService,并调用AppWidgetServiceImpl.onStart()方法,初始化相关管理器,注册四个广播接收器:

第一、注册有关软件包安装、删除、改变的广播,以便我们可以更新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);
第二、当锁定状态发生变化时会播放一些事件:
        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);
第三、暂停或解除暂停应用广播;
        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三个类的关系

    mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);

    mAppWidgetHost = new LauncherAppWidgetHost(this);
    // Host启动监听,监听LauncherProvider中的数据改变
    mAppWidgetHost.startListening();
    ...
}
1、初始化AppWidgetHostd对象,创建用于回调的Callbacks服务类IAppWidgetHost.Stub, 绑定服务bindService,得到IAppWidgetService对象,进行launcher和AppWidgetService之间的调用
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信息保存到系统服务成员变量中,
3、当添加AppWidget时,首页返回到Launcher中的onActivityResult中,在handleActivityResult中
        addAppWidgetImpl(
                        appWidgetId, requestArgs, null,
                        requestArgs.getWidgetHandler(),
                        ON_ACTIVITY_RESULT_ANIMATION_DELAY);
创建添加小部件意图,之后返回到onActivityForResult,调用completeAddAppWidget,通过IAppWidgetService.getAppWidgetInfo,获取AppWidgetProviderInfo,保存到本地数据库中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    }
并创建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;
    }
调用AppWidgetHostView.updateAppWidget(views);更新View到launcher界面上mWorkspace.addInScreen(hostView, launcherInfo);

当AppWidgetProvider获得更新的广播,并执行onUpdate(),onUpdate()中创建了RemoteViews并通过AppWidgetManager.updateAppWidget()更新到AppWidgetService之后,AppWidgetService会通过注册的IAppWidgetHost的回调,执行AppWidgetHost的更新。

总结

加载全部内容

相关教程
猜你喜欢
用户评论