开发者

How to solve the bug in CheckBoxPreference Default Value in android?

Because my mainActivity does not run my Tab2Activity at startup until the user press the setting button to run the PreferenceActivity, therefore i have to first check the audioStatus boolean value in order to avoid unwanted boolean result but after this step i'm kinna lost because of the bug in CheckBoxPreference it gives me...

Now i don't know how to work with the logic comparison to get the audio even without navigating to Tab2Activity? Main problem here i'm facing is working with the logics yet getting the desirable result..

I'm kinna new in java/android and currently creating an car blackbox app can someone help me... Thanks :)

My mainActivity file

if(Tab2Activity.audioPref == false) 
    audioStatus = false;    
else
    audioStatus = Tab2Activity.audioPref; 

if(audioStatus == false)
   mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

if(audioStatus == false)
   mediaRecorder.setAudioEn开发者_StackOverflowcoder(MediaRecorder.AudioEncoder.DEFAULT);

My Tab2Activity.java file

    public static boolean audioPref;
    public static String timeIntervalPref;

    public void getPrefs() {
         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

         audioPref = prefs.getBoolean("AudioPref", true);//Suppose to produce "false" isn't it?
         timeIntervalPref = prefs.getString("TimeIntervalPref", "60000");
    }
}

My xml file

<CheckBoxPreference
                android:title="Audio"
                android:defaultValue="True"
                android:summary="Select w/o Audio when Recording"
                android:key="AudioPref" />


save your settings to a SharedPreferences then read them from there rather than relying on the state of a public boolean in the Tab2Activity.

http://developer.android.com/reference/android/content/SharedPreferences.html

example of use: http://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html

EDIT: I don't know why you are trying to get the preferences from tab2activity. why wouldn't you do the following in mainActivity:

SharedPreferences prefs=PreferenceManager.getDefaultSharedreferences(getBaseContext()); 
audioStatus=prefs.getBoolean("AudioPref",true); // (only use true if you want the default to be true if the value has not yet been set, otherwise you should be doing ("AudioPref",false) )

if(!audioStatus) 
{ 
   mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
   mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
}

The bug you linked describes issues when you try to set default shared preference values to 'false'. If you want that to be the default, then just use "false" as the default value when you go to retrieve the value using getBoolean(string,defValue)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜