我是靠谱客的博主 受伤河马,最近开发中收集的这篇文章主要介绍Android 系统启动WiFi初始化流程,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Android系统服务启动完毕的时候,会去启动其他服务,其中就包括WifiService。

以下均参考Android P源码。

1. 启动WifiService流程

    private static final String WIFI_SERVICE_CLASS =
            "com.android.server.wifi.WifiService";

    /**
     * The main entry point from zygote.
     */
    public static void main(String[] args) {
        new SystemServer().run();
    }

    private void run() {
        ........
        // Start services.
        startOtherServices();
    }

    private void startOtherServices() {
        ........
        mSystemServiceManager.startService(WIFI_SERVICE_CLASS);
        mSystemServiceManager.startService(
            "com.android.server.wifi.scanner.WifiScanningService");
    }

2. WifiService构造函数中会new一个WifiServiceImpl,

public final class WifiService extends SystemService {

    private static final String TAG = "WifiService";
    final WifiServiceImpl mImpl;

    public WifiService(Context context) {
        super(context);
        mImpl = new WifiServiceImpl(context, new WifiInjector(context), new WifiAsyncChannel(TAG));
    }

    @Override
    public void onStart() {
        Log.i(TAG, "Registering " + Context.WIFI_SERVICE);
        publishBinderService(Context.WIFI_SERVICE, mImpl);
    }

    @Override
    public void onBootPhase(int phase) {
        if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
            mImpl.checkAndStartWifi();
        }
    }

    @Override
    public void onSwitchUser(int userId) {
        mImpl.handleUserSwitch(userId);
    }

    @Override
    public void onUnlockUser(int userId) {
        mImpl.handleUserUnlock(userId);
    }

    @Override
    public void onStopUser(int userId) {
        mImpl.handleUserStop(userId);
    }
}

WifiServiceImpl内部的主要成员:

WifiStateMachine

WifiStateMachinePrime

WifiController

ScanRequestProxy

WifiCountryCode

WifiSettingsStore

WifiApConfigStore

    public WifiServiceImpl(Context context, WifiInjector wifiInjector, AsyncChannel asyncChannel) {
        mContext = context;
        mWifiInjector = wifiInjector;
        mClock = wifiInjector.getClock();

        mFacade = mWifiInjector.getFrameworkFacade();
        mWifiMetrics = mWifiInjector.getWifiMetrics();
        mTrafficPoller = mWifiInjector.getWifiTrafficPoller();
        mUserManager = mWifiInjector.getUserManager();
        mCountryCode = mWifiInjector.getWifiCountryCode();
        mWifiStateMachine = mWifiInjector.getWifiStateMachine();
        mWifiStateMachinePrime = mWifiInjector.getWifiStateMachinePrime();
        mWifiStateMachine.enableRssiPolling(true);
        mScanRequestProxy = mWifiInjector.getScanRequestProxy();
        mSettingsStore = mWifiInjector.getWifiSettingsStore();
        mPowerManager = mContext.getSystemService(PowerManager.class);
        mAppOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
        mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        mWifiLockManager = mWifiInjector.getWifiLockManager();
        mWifiMulticastLockManager = mWifiInjector.getWifiMulticastLockManager();
        HandlerThread wifiServiceHandlerThread = mWifiInjector.getWifiServiceHandlerThread();
        mClientHandler = new ClientHandler(TAG, wifiServiceHandlerThread.getLooper());
        mWifiStateMachineHandler = new WifiStateMachineHandler(TAG,
                wifiServiceHandlerThread.getLooper(), asyncChannel);
        mWifiController = mWifiInjector.getWifiController();
        mWifiBackupRestore = mWifiInjector.getWifiBackupRestore();
        mWifiApConfigStore = mWifiInjector.getWifiApConfigStore();
        mPermissionReviewRequired = Build.PERMISSIONS_REVIEW_REQUIRED
                || context.getResources().getBoolean(
                com.android.internal.R.bool.config_permissionReviewRequired);
        mWifiPermissionsUtil = mWifiInjector.getWifiPermissionsUtil();
        mLog = mWifiInjector.makeLog(TAG);
        mFrameworkFacade = wifiInjector.getFrameworkFacade();
        mIfaceIpModes = new ConcurrentHashMap<>();
        mLocalOnlyHotspotRequests = new HashMap<>();
        enableVerboseLoggingInternal(getVerboseLoggingLevel());
        mRegisteredSoftApCallbacks = new HashMap<>();

        mWifiInjector.getWifiStateMachinePrime().registerSoftApCallback(new SoftApCallbackImpl());
        mPowerProfile = mWifiInjector.getPowerProfile();
    }

3. 等待 SystemService.PHASE_SYSTEM_SERVICES_READY的时候,会调用 WifiServiceImpl#checkAndStartWifi(), 获取Wi-Fi开关,持久化存储的值。

存在SettingsProvider中  "wifi_on" Settings.Global.getInt(cr, Settings.Global.WIFI_ON) , 如果上次Wi-Fi是开着的,则调用setWifiEnabled,走Wi-Fi打开流程。

具体流程如下图:

如果有需求要求默认每次开机WiFi都要打开,可以在WifiServiceImpl.checkAndStartWifi中添加修改,强制每次都去打开,如下:

    /**
     * Check if we are ready to start wifi.
     *
     * First check if we will be restarting system services to decrypt the device. If the device is
     * not encrypted, check if Wi-Fi needs to be enabled and start if needed
     *
     * This function is used only at boot time.
     */
    public void checkAndStartWifi() {
        // First check if we will end up restarting WifiService
        if (mFrameworkFacade.inStorageManagerCryptKeeperBounce()) {
            Log.d(TAG, "Device still encrypted. Need to restart SystemServer.  Do not start wifi.");
            return;
        }

        // Check if wi-fi needs to be enabled
        //boolean wifiEnabled = mSettingsStore.isWifiToggleEnabled();
        boolean wifiEnabled = true;
        Slog.i(TAG, "WifiService starting up with Wi-Fi " +
                (wifiEnabled ? "enabled" : "disabled"));
        ........
    }

最后

以上就是受伤河马为你收集整理的Android 系统启动WiFi初始化流程的全部内容,希望文章能够帮你解决Android 系统启动WiFi初始化流程所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部