Android10开机向导中复用设置中的Wifi界面

很多时候我们需要定制开机向导,在开机向导界面我们一般会实现联网和设置时间等功能,考虑复用与稳定性问题,我们最好复用设置中的WiFi设置和日期设置。但是设置中的wifi设置界面默认是没有下一步按钮的,这会让用户感觉很奇怪。

在以前7.0的代码中设置中有WifiSetupActivity 可让我们方便的集成,但在Android10上我们却找不到了。

    <activity android:name=".wifi.WifiSetupActivity"
282                 android:taskAffinity="com.android.wizard"
283                 android:theme="@style/SetupWizardDisableAppStartingTheme"
284                 android:label="@string/wifi_setup_wizard_title"
285                 android:icon="@drawable/empty_icon"
286                 android:clearTaskOnLaunch="true"
287                 android:windowSoftInputMode="adjustNothing">
288             <intent-filter android:priority="1">
289                 <action android:name="com.android.net.wifi.SETUP_WIFI_NETWORK" />
290                 <category android:name="android.intent.category.DEFAULT" />
291             </intent-filter>
292             <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
293                 android:value="true" />
294         </activity>

那么Android原生的开机向导是如何处理的呢?

Android原生的开机向导是在packages/apps/Provision目录下,里面啥都没干,仅是设置了两个标志位后disable自身应用就finish掉了,用户感知不到。

    private void setProvision() {
        // Add a persistent setting to allow other apps to know the device has been provisioned.
        Settings.Global.putInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1);
        Settings.Secure.putInt(getContentResolver(), Settings.Secure.USER_SETUP_COMPLETE, 1);

        // remove this activity from the package manager.
        PackageManager pm = getPackageManager();
        ComponentName name = new ComponentName(this, DefaultActivity.class);
        pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

        // terminate the activity.
        finish();
    }

可用命令读写相关标志位状态:

adb shell settings put global device_provisioned 1
adb shell settings put secure  user_setup_complete 1

adb shell settings get  secure user_setup_complete
adb shell "settings get global device_provisioned"

其实原生设置界面中还是给我们留有接口,只是方式不一样。

在WifiSettings中搜索Setup,进一步我们可以发现如下一段代码:

        mEnableNextOnConnection = intent.getBooleanExtra(EXTRA_ENABLE_NEXT_ON_CONNECT, false);
        if (mEnableNextOnConnection) {
            if (hasNextButton()) {
                final ConnectivityManager connectivity = (ConnectivityManager)
                        getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connectivity != null) {
                    NetworkInfo info = connectivity.getNetworkInfo(
                            ConnectivityManager.TYPE_WIFI);
                    changeNextButtonState(info.isConnected());
                }
            }
        }

这里说明了wifi界面最下面的”上一步和下一步“按钮是如何控制的。

另外在SettingsActivity.java中会通过启动时传的EXTRA_PREFS_SHOW_BUTTON_BAR 控制的。


        // see if we should show Back/Next buttons
        if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
            View buttonBar = findViewById(R.id.button_bar);
            if (buttonBar != null) {
                buttonBar.setVisibility(View.VISIBLE);

                Button backButton = findViewById(R.id.back_button);
                backButton.setOnClickListener(v -> {
                    setResult(RESULT_CANCELED, null);
                    finish();
                });
                Button skipButton = findViewById(R.id.skip_button);
                skipButton.setOnClickListener(v -> {
                    setResult(RESULT_OK, null);
                    finish();
                });
                mNextButton = findViewById(R.id.next_button);
                mNextButton.setOnClickListener(v -> {
                    setResult(RESULT_OK, null);
                    finish();
                });

所以我们在复用wifiSettings界面时,可用如下方法

 Intent i = new Intent(Settings.ACTION_WIFI_SETTINGS);
        i.putExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, true);
        i.putExtra(EXTRA_ENABLE_NEXT_ON_CONNECT, true);
        startActivityForResult(i, REQUEST_CODE_WIFI);

日期设置界面同理,不过不需要传EXTRA_ENABLE_NEXT_ON_CONNECT。

如果我们需要对返回的结果做相关处理,可以复写onActivityResult,在这里做相关操作。

这样在开机向导中WiFi设置界面最下面就可以显示出上一步和下一步两个按钮了,如果嫌原生界面不好看,就只能自行修改settings了。

另外调试过程中 adb shell pm clear packageName 可快速清除开机向导保存的数据,加快调试速度。

Nice。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值