很多时候我们需要定制开机向导,在开机向导界面我们一般会实现联网和设置时间等功能,考虑复用与稳定性问题,我们最好复用设置中的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。