比如在Setting的显示中添加“近距离提醒”选项开关,这时需要定义一个系统全局变量来保存“近距离提醒”打开或关闭的状态。
然后就可以在其他地方通过获取“近距离提醒”的状态做不同的逻辑处理。
1.在frameworks/base/core/java/android/provider/Settings.java添加自定义系统全局变量。
public static final class Secure extends NameValueTable {
.....
/** @hide */
public static final String DISPLAY_CLOSE_REMINDER= "seewo.close.reminder";
....
注意:需要加入/** @hide */,否则编译会报错。
2.vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values-zh-rCN/strings.xml
+ <string name="close_reminder_title">Close reminder</string>
3.vendor/mediatek/proprietary/packages/apps/MtkSettings/res/xml/display_settings.xml
+ <SwitchPreference
+ android:key="close_reminder"
+ android:title="@string/close_reminder_title"/>
4.vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/DisplaySettings.java
+import com.android.settings.display.CloseReminderPreferenceController;
@@ -100,6 +101,7 @@ public class DisplaySettings extends DashboardFragment {
+ controllers.add(new CloseReminderPreferenceController(context));
controllers.add(new BrightnessLevelPreferenceController(context, lifecycle));
5.添加新文件vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/display/CloseReminderPreferenceController.java
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.android.settings.display;
import android.content.Context;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
public class CloseReminderPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private static final String KEY_CLOSE_REMINDER_NAME = "close_reminder";
public CloseReminderPreferenceController(Context context) {
super(context);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return KEY_CLOSE_REMINDER_NAME;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean value = (Boolean) newValue;
Settings.Secure.putInt(mContext.getContentResolver(),
KEY_CLOSE_REMINDER_NAME, value ? 1 : 0);
if (value) {
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.DISPLAY_CLOSE_REMINDER,
1);//当开关为打开时,保存自定义系统变量值为 1
} else {
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.DISPLAY_CLOSE_REMINDER,
0);//当开关为关闭时,保存自定义系统变量值为 0
}
return true;
}
@Override
public void updateState(Preference preference) {
int value = Settings.Secure.getInt(mContext.getContentResolver(),
KEY_CLOSE_REMINDER_NAME, 1);
((SwitchPreference) preference).setChecked(value == 1);
}
}
6.获取自定义系统属性变量值
Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.DISPLAY_CLOSE_REMINDER,1)