I have some preferences in my activity and I would like to achieve that, depending on these preferences, some services will be initiated or not.
I am trying to implement and onSharedPreferenceChangedmethod, but it never gets called (even if I have assigned it to a field).
The code is the following:
public class MtprojectActivity extends Activity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
callsCheckbox = prefs.getBoolean("callsLogChk", true);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (!started) {
startCallsService();
bindCallsService();
} else {
releaseCallsService();
stopCallsService();
}
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
Thanks a lot in advance!
Are you sure it isn't being called or is it just a case of it not doing what you expect? I may be wrong but shouldn't the code look like this...
if (key.equals("callsLogChk")) {
if (!started) { // Check NOT started ???
startCallsService();
bindCallsService();
}
else { // The 'else' should compliment the check for !started ???
releaseCallsService();
stopCallsService();
}
}
Sorry, as I don't know your code I may be totally wrong but as you've posted it, it doesn't look quite right to me.
EDIT:
Also, are you making sure you call the SharedPreferences.Editor.commit() method when you make the change to the preference?
Related
I have set to break on both read and write to a field, as shown below:
The problem is that the code never breaks when the field was access/modified when I know for a fact that the field was being accessed, and that breakpoints set in methods work as intended. How do I make it work?
Maybe you can try this: it's not a perfect solution, but a compromise one.
public class MainActivity extends Activity {
private boolean value = test();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (value) {
// do sth.
value = false;
}
}
private boolean test() {
return false;
}
}
create break point at return false line. But you may want to delete the extra code when the debugging is done.
I am new to Android Development.
I recently created a slideshow live wallpaper which would change the image in every a few seconds depending on the user's selection. I also added the login, create password, etc. as learning purpose.
I have not seen this message before until I just updated the Eclipse. I am not if it has to do with Eclipse or my codes.
This app crashes on my Galaxy Note 2 sometimes. There must be something wrong with the codes, but I can't figure out what causes it....
I read about Asyntask, but I don't know how to approach it.
Could you someone please help me on this?
Thank you.
prefs class:
public class prefs extends PreferenceActivity implements
SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences mSharedPreferences;
private CheckBoxPreference pref_checkbox;
protected static final String TAG = null;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(main_activity.SHARED_PREFS_NAME);
addPreferencesFromResource(R.xml.wallpaper_settings);
getPreferenceManager().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
I have a bunch of these codes for different buttons just right below the above Oncreate.
Is it the cause of the frame skipping problems or anything wrong with them?
Preference button1 = (Preference)findPreference("button1");
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
//code for login_password
return true;
}
});
Preference button2 = (Preference)findPreference("button2");
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
//code for create_password
return true;
}
});
Preference button3 = (Preference)findPreference("button3");
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
//code for reset_password
return true;
}
});
So on....
Edit: fixed errors.
I had a similar issue when I started with Android development. The app crashed everytime I called drawBitmap, or something like rotate an image. I had to move everything that manipulated images to an asynctask, and the app worked well after. Perhaps this is a solution for you as well?
I recently imported the HoloEverywhere library from GitHub into Eclpse and have begun using it in an app that has already been working. Overall I am happy with the library and things have been going well.
I am trying to use the org.holoeverywhere.preference.PreferenceActivity in place of my old PreferenceActivity. The UI looks as it should, but I noticed that onSharedPreferenceChanged() never gets called anymore. What am I doing wrong?
AFAIK I am using the library as intended. I have barely changed anything from my old version to the new version using HoloEverywhere. While there are many related questions on SO, I could not find anything that addresses my problem.
Relevant code posted below:
import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.preference.Preference;
import org.holoeverywhere.preference.PreferenceFragment;
import org.holoeverywhere.preference.PreferenceManager;
import org.holoeverywhere.preference.PreferenceScreen;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.preference.SharedPreferences.Editor;
import org.holoeverywhere.preference.SharedPreferences.OnSharedPreferenceChangeListener;
public class SettingsActivity extends org.holoeverywhere.preference.PreferenceActivity implements SyncManager.SyncProgressListener, SharedPreferences.OnSharedPreferenceChangeListener
{
private static SharedPreferences prefs;
#Override
public void onCreate(Bundle savedInstanceState)
{
prefs = PreferenceManager.getDefaultSharedPreferences( this );
}
public void onSharedPreferenceChanged(SharedPreferences prefs, String key)
{
// do some really important stuff here
}
public static class DisplaySetttingsFragment extends PreferenceFragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate( savedInstanceState );
getActivity().setTitle( getString( R.string.pref_display_title ) );
addPreferencesFromResource( R.xml.display_preferences );
}
#Override
public void onResume()
{
super.onResume();
prefs.registerOnSharedPreferenceChangeListener( (SettingsActivity) getActivity() );
}
#Override
public void onPause()
{
super.onPause();
prefs.unregisterOnSharedPreferenceChangeListener( (SettingsActivity) getActivity() );
}
}
}
Update: An example where I change and commit preferences and I would expect onSharedPreferenceChanged() to be called
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
{
String resetString = getString( R.string.pref_key_reset_display );
String key = preference.getKey();
if ( key != null && key.equals( resetString ) )
{
prefs.edit().
putBoolean( getString( R.string.pref_key_reset_display ), true ).commit();
}
return super.onPreferenceTreeClick( preferenceScreen, preference );
}
Update: I do not believe this is a problem of my preferences not having a registered listener (in this case my SettingsActivity) at the time of a commit() or apply(). I am able to debug and see that the WeakHashMap inside prefs for listeners always has my activity as a member. I have tried creating an global variable that is a listener, but it makes no difference.
The might be missing the below calls: this might solve your problem.
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
/* (non-Javadoc)
* #see android.app.Activity#onPause()
*/
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
try to call
prefs.registerOnSharedPreferenceChangeListener(this)
in onCreate in SettingsActivity. Calling it in Fragment, in inner class, will destroy the listeners when inner fragment is Paused, I suppose
Check the preference.xml. All the preferences have a defined key value? If not the callback will not be fired.
I had a similar problem. For me the listener was called for a standard property but nor for one set with the editor.
The solution came from the doku for the listener
This may be called even if a preference is set to its existing value.
The solution for me was setting the boolean first to the wrong and then to the correct value(and committing each change).
I need some help with an android project I am working on. I am trying to use switch preferences to send a certain text. Basically, it the user switches the switch from off to on, I want the phone to send a text saying "on". Then when the user turns the switch from on to off, it sends a text saying "off". All I need is to be able to see what the current state of the switch is and then if it's off, call a "turn on" method and vice-versa.
I've never asked a question like this, so I don't really know what part of my code to post.(If asked, I can post most of my code.) I think it has something to do with the onPreferenceChangeListener, but I'm not sure how to implement it. Any ideas?
Edit: Here is the main activity class:
public class MainActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
public static final String KEY_ROOM1_SWITCH = "switch_room_1";
private SwitchPreference mSwitchPreference1;
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
//This is a sample of one of 4 switches that are being used. They are all basically the same, but with different variables
if (key.equals(KEY_ROOM1_SWITCH)) {
boolean checkedornot1;
SharedPreferences myPreference=PreferenceManager.getDefaultSharedPreferences(this);
checkedornot1 = myPreference.getBoolean("switch_room_1", false);
if (checkedornot1 = true)
mSwitchPreference1.setChecked(true);
else
mSwitchPreference1.setChecked(false);
}
}
}
Do I need to grab the value that is stored in the shared preferences and make my choice based on that? or is there something else I am missing?
Edit your class that extends PreferenceActivity and add the private variable: private OnSharedPreferenceChangeListener listener;
Create and register your listener within the onResume method:
public void onResume() {
super.onResume();
listener = new OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
if (key.contains("your switchpreference key name")
if (sp.getBoolean("your switchpreference key name",false) {
sendOnSMS();
} else {
sendOffSMS();
}
}
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(listener);
}
Unregister your listener within the onPause method:
public void onPause() {
super.onPause();
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(listener);
}
Implement the sendOnSMS and sendOffSMS methods.
I'm working on listpreferences and I have implemented the OnSharedPreferenceChangeListener to change the text on a textview based on which item is selected on the listpreference. However it doesn't register any changes and I have run out of ideas so any help would be appreciated. Thanks
public class pref extends PreferenceActivity implements OnSharedPreferenceChangeListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
setContentView(R.layout.main);
TextView text = (TextView) findViewById(R.id.textView1);
String keys = null;
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.registerOnSharedPreferenceChangeListener(this);
keys = pref.getString("listPref1", "");
if (keys == "ON")
{
text.setText("ON");
}
else if (keys == "OFF")
{
text.setText("OFF");
}
else{
text.setText("Choose One");
}
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
//What code to I put in here to have the listener register the change
}
}
Your code looks good for registering to hear preference changes, you need to put some code in your onSharedPreferenceChanged method to process the preferences as they are changed. Remember that you will get called back when any preference changes, so you need to do some filtering in the method to make sure it's what you're interested in.
Could be something as simple as:
if(key.equals("listPref1"))
text.setText("ON"); // Need to make text a class variable
Thank you all for your help, I've sorted most of it out now here is the code
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
TextView text = (TextView) findViewById(R.id.textView1);
if (prefs.getString("listPref1", "").equals("OFF")) {
text.setText("goodbye");
} else if (prefs.getString("listPref1", "").equals("ON")) {
text.setText("hello");
}
prefs.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
TextView text = (TextView) findViewById(R.id.textView1);
if (prefs.getString("listPref1", "").equals("OFF")) {
text.setText("goodbye");
} else if (prefs.getString("listPref1", "").equals("ON")) {
text.setText("hello");
}
}
}
As you can see I have to repeat the if/else statement, if I put it only in the listener nothing gets until a change is made and for if it's only outside the listener it only gets read when the app is first run and changes are not registered.
Isn't there a code to get the listener to notify a change has been made and the code gets re-run?
Your code looks like it should work: you can try logging out to ensure its doing what it should. Move all the conditional code into the listener.
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
android.util.Log.v("PREFERENCE CHANGE", "[" + key + "]");
if ("listPref1".equals(key)) {
String keys = pref.getString("listPref1", "");
if (keys == "ON") {
text.setText("ON");
} else if (keys == "OFF") {
text.setText("OFF");
} else {
text.setText("Choose One");
}
}
}
You don't have any code to actually update your SharedPreferences. The listener will only be triggered when your pref object is edited. Example...
pref.edit().putString("listPref1", "ONE").commit();