我是Android的初学者,我尝试使用首选项活动制作简单的手电筒应用程序。所以我的问题是;我想让光线和声音设置可选,并带有复选框。android复选框首选项获取并设置布尔值
的settings.xml
<PreferenceCategory
android:title="@string/sct_behaviour">
<CheckBoxPreference
android:key="pref_light"
android:title="@string/st_pref_light"
android:summary="@string/ss_pref_light"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="pref_switch_sound"
android:title="@string/st_pref_sound"
android:summary="@string/ss_pref_sound"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="pref_notification_sound"
android:title="@string/st_pref_notification_sound"
android:summary="@string/ss_pref_notification_sound"
android:defaultValue="true"
/>
settings.java
public class Settings extends PreferenceActivity{
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
CheckBoxPreference pLight = (CheckBoxPreference)getPreferenceManager().findPreference("pref_light");
pLight.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean myValue = (Boolean) newValue;
if(myValue){
}
//NEED SOME HELP IN THERE
return true;
}
});
我turnOnFlash功能;
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
// play sound
playSound();
}
}
在onStart功能
@Override
protected void onStart() {
super.onStart();
if(VERBOSE) Log.v(TAG, "++ ON START ++");
// on starting the app get the camera params
getCamera();
turnOnFlash(value);
}
我想通过用户的选择来设置此true或false值在onStart周期。并不知道如何做这个实现。