首页 > 代码库 > Android Setting显示开发者选项的逻辑
Android Setting显示开发者选项的逻辑
默认User版本没有“开发者选项”,我们可以通过点击版本号7次来显示开发者模式,今天来看下代码中是如何处理的:
首先是packages/apps/Settings/src/com/android/settings/Settings.java的初始化处理:
布尔值showDev是否默认显示“开发者选项”:
final boolean showDev = mDevelopmentPreferences.getBoolean( DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"));
如果想让开发者选项默认显示,不需要点击7次版本号,可直接将showDev置为true即可:
final boolean showDev = true;
如果showDev为false,则在如下代码中隐藏“开发者选项”:
if (id == R.id.development_settings) { if (!showDev) { target.remove(i); } }
代码路径:packages/apps/Settings/src/com/android/settings/DeviceInfoSettings.java
首先是定义点击次数:
static final int TAPS_TO_BE_A_DEVELOPER = 7;
点击版本号的处理逻辑,若mDevHitCountdown小于0,则表示”开发者选项“已经显示,参考Resume函数的处理:
if (preference.getKey().equals(KEY_BUILD_NUMBER)) { // Don't enable developer options for secondary users. if (UserHandle.myUserId() != UserHandle.USER_OWNER) return true; if (mDevHitCountdown > 0) { if (mDevHitCountdown == 1) { if (super.ensurePinRestrictedPreference(preference)) { return true; } } mDevHitCountdown--; if (mDevHitCountdown == 0) { getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE, Context.MODE_PRIVATE).edit().putBoolean( DevelopmentSettings.PREF_SHOW, true).apply(); if (mDevHitToast != null) { mDevHitToast.cancel(); } mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_on, Toast.LENGTH_LONG); mDevHitToast.show(); } else if (mDevHitCountdown > 0 && mDevHitCountdown < (TAPS_TO_BE_A_DEVELOPER-2)) { if (mDevHitToast != null) { mDevHitToast.cancel(); } mDevHitToast = Toast.makeText(getActivity(), getResources().getQuantityString( R.plurals.show_dev_countdown, mDevHitCountdown, mDevHitCountdown), Toast.LENGTH_SHORT); mDevHitToast.show(); } } else if (mDevHitCountdown < 0) { if (mDevHitToast != null) { mDevHitToast.cancel(); } mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_already, Toast.LENGTH_LONG); mDevHitToast.show(); } }
Resume函数:
@Override public void onResume() { super.onResume(); PreferenceGroup parentPreference = getPreferenceScreen(); mDevHitCountdown = getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE, Context.MODE_PRIVATE).getBoolean(DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng")) ? -1 : TAPS_TO_BE_A_DEVELOPER; mDevHitToast = null; }
转载请注明出处:周木水的CSDN博客 http://blog.csdn.net/zhoumushui
我的GitHub:周木水的GitHub https://github.com/zhoumushui
Android Setting显示开发者选项的逻辑
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。