在settings里单独添加一个activity来实现的方法就不说了,我这里主要是在settings列表添加一个Preference,extends RadioButtonPickerFragment来实现,然后在其他应用地方调用时,用activity套壳跳转来实现。
添加一列RadioButton单选,选择后实现相应功能。我做的是实现battry mode的相关功能,这里就以此为例。
在想要添加功能条目的相应的setting的xml里添加对应的Preference
<Preference
android:key="charging_mode"
android:title="@string/charging_state"
android:fragment="com.android.settings.fuelgauge.batterysaver.BatteryChargingState"/>
代码如下(示例):
package com.android.settings.fuelgauge.batterysaver;
import com.android.settings.R;
import com.android.settings.widget.RadioButtonPickerFragment;
import com.android.settingslib.widget.CandidateInfo;
import android.graphics.drawable.Drawable;
import android.app.settings.SettingsEnums;
import android.content.Context;
import java.util.ArrayList;
import java.util.List;
import android.util.Log;
import android.os.UserHandle;
import android.provider.Settings;
import android.os.SystemProperties;
/**
* author : suntianhai
* e-mail : [email protected]
* time : 2023/08/1
* desc : add for set Battery Charging mode
* version: 1.0
*
*/
public class BatteryChargingState extends RadioButtonPickerFragment {
private static final String TAG = "BatteryChargingState";
// charging_slow charging_normal
private String[] mEntries;
// 1 0
private String[] mValues;
public BatteryChargingState() { }
@Override
public int getMetricsCategory() {
return SettingsEnums.BATTERY_CHARGING_STATE;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.battery_charging_state_settings;
}
@Override
protected List<? extends CandidateInfo> getCandidates() {
final List<CandidateInfo> candidates = new ArrayList<>();
if (mEntries == null || mValues == null) {
return candidates;
}
for (int i = 0; i < mValues.length; i++) {
candidates.add(new ChargingStateCandidateInfo(mEntries[i], mValues[i], true));
}
return candidates;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mEntries = getContext().getResources().getStringArray(
R.array.charging_mode);
mValues = getContext().getResources().getStringArray(
R.array.charging_mode_value);
}
@Override
public void onStart() {
super.onStart();
}
@Override
protected String getDefaultKey() {
return mValues[0];
}
@Override
public void onStop() {
super.onStop();
}
@Override
protected boolean setDefaultKey(String key) {
String charge_mode = SystemProperties.get("persist.sys.charge_mode");
switch (key) {
case "0"://slow
break;
case "1"://normal
break;
}
return true;
}
private static class ChargingStateCandidateInfo extends CandidateInfo {
private final CharSequence mLabel;
private final String mKey;
ChargingStateCandidateInfo(CharSequence label, String key, boolean enabled) {
super(enabled);
mLabel = label;
mKey = key;
}
@Override
public CharSequence loadLabel() {
return mLabel;
}
@Override
public Drawable loadIcon() {
return null;
}
@Override
public String getKey() {
return mKey;
}
}
}
battery_charging_state_settings.xml 只是显示个标题:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/charging_state"/>
其中关于getMetricsCategory的设置,只需要在项目对应的settings_enums.proto里添加一个自定义的值就可以了
BATTERY_CHARGING_STATE = 2023;
<activity android:name="com.android.settings.fuelgauge.batterysaver.BatsActivity"
android:exported="true"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
intent-filter>
activity>
package com.android.settings.fuelgauge.batterysaver;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.android.settings.core.SettingsBaseActivity;
import com.android.settings.fuelgauge.batterysaver.BatteryChargingState;
import android.view.View;
import android.widget.Toolbar;
import com.android.settings.R;
/**
* author : suntianhai
* e-mail : [email protected]
* time : 2023/08/01
* desc : add for set Battery Charging mode
* version: 1.0
*
*/
public class BatsActivity extends SettingsBaseActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new BatteryChargingState())
.commit();
}
}
Intent intent = new Intent();
intent.setClassName(“com.android.settings”,
“com.android.settings.fuelgauge.batterysaver.BatsActivity”);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
主要是想在RadioButton下面添加一些提示提醒隐私政策什么的
import androidx.preference.PreferenceScreen;
import com.android.settingslib.widget.FooterPreference;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
private Context mContext;
private FooterPreference mPrivacyPreference;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
mEntries = getContext().getResources().getStringArray(
R.array.charging_mode);
mValues = getContext().getResources().getStringArray(
R.array.charging_mode_value);
mPrivacyPreference = new FooterPreference(context);
}
@Override
public void updateCandidates() {
final String defaultKey = getDefaultKey();
final PreferenceScreen screen = getPreferenceScreen();
screen.removeAll();
final List<? extends CandidateInfo> candidateList = getCandidates();
if (candidateList == null) {
return;
}
for (CandidateInfo info : candidateList) {
SelectorWithWidgetPreference pref =
new SelectorWithWidgetPreference(getPrefContext());
bindPreference(pref, info.getKey(), info, defaultKey);
screen.addPreference(pref);
}
mPrivacyPreference = new FooterPreference(mContext);
mPrivacyPreference.setIcon(R.drawable.ic_privacy_shield_24dp);
mPrivacyPreference.setTitle("标题");
mPrivacyPreference.setSelectable(false);
mPrivacyPreference.setLayoutResource(R.layout.preference_footer);
screen.addPreference(mPrivacyPreference);
}
其实这些都可以直接添加到activity,在里面做自己想要的功能,我只是想保持setting的队形,不想setting这个功能在跳转的时候膈应。