zoukankan      html  css  js  c++  java
  • android源码中修改wifi热点默认始终开启

    在项目frameworksasewifijavaandroid etwifiWifiStateMachine.java里面,有如下的代码,是设置wifi热点保持状态的:如下: 

     private class HotspotAutoDisableObserver extends ContentObserver {
            public HotspotAutoDisableObserver(Handler handler) {
                super(handler);
                mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor(
                    Settings.System.WIFI_HOTSPOT_AUTO_DISABLE), false, this);
            }
    
            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                mDuration = Settings.System.getInt(mContext.getContentResolver(), Settings.System.WIFI_HOTSPOT_AUTO_DISABLE,
                        Settings.System.WIFI_HOTSPOT_AUTO_DISABLE_FOR_FIVE_MINS);
                if (mDuration != Settings.System.WIFI_HOTSPOT_AUTO_DISABLE_OFF && mPluggedType == 0) {
                    if (mClientNum == 0 && WifiStateMachine.this.getCurrentState() == mTetheredState) {
                        mAlarmManager.cancel(mIntentStopHotspot);
                        Xlog.d(TAG, "Set alarm for setting changed, mDuration:" + mDuration);
                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
                            System.currentTimeMillis() + mDuration * HOTSPOT_DISABLE_MS, mIntentStopHotspot);
                    }
                } else {
                    mAlarmManager.cancel(mIntentStopHotspot);
                }
            }
        }
    

    修改默认值为始终:

    mDuration = Settings.System.getInt(mContext.getContentResolver(), Settings.System.WIFI_HOTSPOT_AUTO_DISABLE,
                        Settings.System.WIFI_HOTSPOT_AUTO_DISABLE_OFF);


    最关键的部分是修改初始化:
        private void initializeExtra() {
            PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
            mDhcpWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DHCP_WAKELOCK");
            mDhcpWakeLock.setReferenceCounted(false);
    
            mHotspotNative = new WifiNative("ap0");
            mHotspotMonitor = new WifiMonitor(this, mHotspotNative);
    
            HandlerThread wifiThread = new HandlerThread("WifiSMForObserver");
            wifiThread.start();
    
            mHotspotAutoDisableObserver = new HotspotAutoDisableObserver(new Handler(wifiThread.getLooper()));
            Intent stopHotspotIntent = new Intent(ACTION_STOP_HOTSPOT);
            mIntentStopHotspot = PendingIntent.getBroadcast(mContext, STOP_HOTSPOT_REQUEST, stopHotspotIntent, 0);
            //mDuration = Settings.System.getInt(mContext.getContentResolver(), Settings.System.WIFI_HOTSPOT_AUTO_DISABLE,
            //            Settings.System.WIFI_HOTSPOT_AUTO_DISABLE_FOR_FIVE_MINS);
            mDuration = Settings.System.getInt(mContext.getContentResolver(), Settings.System.WIFI_HOTSPOT_AUTO_DISABLE,
                        Settings.System.WIFI_HOTSPOT_AUTO_DISABLE_OFF);//---------修改为默认常开
    
            // M: For stop scan after screen off in disconnected state feature @{
            Intent stopScanIntent = new Intent(ACTION_STOP_SCAN, null);
            mStopScanIntent = PendingIntent.getBroadcast(mContext, STOPSCAN_REQUEST, stopScanIntent, 0);
    
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("com.mtk.beamplus.activated");
            intentFilter.addAction("com.mtk.beamplus.deactivated");
            intentFilter.addAction(ACTION_STOP_HOTSPOT);
            intentFilter.addAction(IWifiFwkExt.AUTOCONNECT_SETTINGS_CHANGE);
            intentFilter.addAction(DisplayManager.ACTION_WIFI_DISPLAY_STATUS_CHANGED);
            intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
            intentFilter.addAction(Intent.ACTION_DUAL_SIM_MODE_CHANGED);
            intentFilter.addAction(ACTION_STOP_SCAN);
    
            final boolean isHotspotAlwaysOnWhilePlugged = mContext.getResources().getBoolean(
                    com.mediatek.internal.R.bool.is_mobile_hotspot_always_on_while_plugged);
            Xlog.d(TAG, "isHotspotAlwaysOnWhilePlugged:" + isHotspotAlwaysOnWhilePlugged);
            if (isHotspotAlwaysOnWhilePlugged) {
                intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
            }
    
            BroadcastReceiver receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    Xlog.d(TAG, "onReceive, action:" + action);
                    if (action.equals("com.mtk.beamplus.activated")) {
                        mBeamPlusStarted.set(true);
                        sendMessage(M_CMD_UPDATE_BGSCAN);
                    } else if (action.equals("com.mtk.beamplus.deactivated")) {
                        mBeamPlusStarted.set(false);
                        sendMessage(M_CMD_UPDATE_BGSCAN);
                    } else if (action.equals(ACTION_STOP_HOTSPOT)) {
                        mWifiManager.setWifiApEnabled(null, false);
                        int wifiSavedState = 0;
                        try {
                            wifiSavedState = Settings.Global.getInt(mContext.getContentResolver(),
                                Settings.Global.WIFI_SAVED_STATE);
                        } catch (Settings.SettingNotFoundException e) {
                            Xlog.e(TAG, "SettingNotFoundException:" + e);
                        }
                        Xlog.d(TAG, "Received stop hotspot intent, wifiSavedState:" + wifiSavedState);
                        if (wifiSavedState == 1) {
                            mWifiManager.setWifiEnabled(true);
                            Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.WIFI_SAVED_STATE, 0);
                        }
                    } else if (action.equals(IWifiFwkExt.AUTOCONNECT_SETTINGS_CHANGE)) {
                        sendMessage(M_CMD_UPDATE_SETTINGS);
                    } else if (action.equals(DisplayManager.ACTION_WIFI_DISPLAY_STATUS_CHANGED)) {
                        WifiDisplayStatus status = (WifiDisplayStatus)intent.getParcelableExtra(
                            DisplayManager.EXTRA_WIFI_DISPLAY_STATUS);
                        Xlog.d(TAG, "Received ACTION_WIFI_DISPLAY_STATUS_CHANGED.");
                        setWfdConnected(status);
                        sendMessage(M_CMD_UPDATE_BGSCAN);
                    } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                        if (isHotspotAlwaysOnWhilePlugged) {
                            int pluggedType = intent.getIntExtra("plugged", 0);
                            Xlog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType:" + pluggedType + ", mPluggedType:" + mPluggedType);
                            if (mPluggedType != pluggedType) {
                                mPluggedType = pluggedType;
                                if (mDuration != Settings.System.WIFI_HOTSPOT_AUTO_DISABLE_OFF && mPluggedType == 0) {
                                    if (mClientNum == 0 && WifiStateMachine.this.getCurrentState() == mTetheredState) {
                                        mAlarmManager.cancel(mIntentStopHotspot);
                                        Xlog.d(TAG, "Set alarm for ACTION_BATTERY_CHANGED changed, mDuration:" + mDuration);
                                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
                                            System.currentTimeMillis() + mDuration * HOTSPOT_DISABLE_MS, mIntentStopHotspot);
                                    }
                                } else {
                                    mAlarmManager.cancel(mIntentStopHotspot);
                                }
                            }
                        }
                    } else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
                        String iccState = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
                        Xlog.d(TAG, "iccState:" + iccState);
                        if (!iccState.equals(IccCardConstants.INTENT_VALUE_ICC_LOADED)) {
                            return;
                        }
                        sendMessage(M_CMD_UPDATE_COUNTRY_CODE);
                    } else if (action.equals(Intent.ACTION_DUAL_SIM_MODE_CHANGED)) {
                        sendMessage(M_CMD_UPDATE_COUNTRY_CODE);
                    } else if (action.equals(ACTION_STOP_SCAN)) {
                        sendMessage(M_CMD_SLEEP_POLICY_STOP_SCAN);
                    }
                }
            };
            mContext.registerReceiver(receiver, intentFilter);
    
            mPppoeInfo = new PPPOEInfo();
            mPppoeLinkProperties = new LinkProperties();
        }
    

      

    另外,System.xxxx的常量定义在源码中的路径:项目frameworksasecorejavaandroidproviderSettings.java

  • 相关阅读:
    51nod 2080 最长上升子序列
    common js
    es Module
    git关于分支的常用操作
    react实现浏览器的返回、前进、刷新,关闭拦截
    Blob,ArrayBuffer,FileReader,FormData,Buffer的理解
    memo、useCallback、useMemo三者的区别
    npm 和 yarn的全局安装位置
    react中单行文本溢出省略号
    react中基于styled-components组件的一像素边框问题
  • 原文地址:https://www.cnblogs.com/suxiaoqi/p/5619741.html
Copyright © 2011-2022 走看看