0

I'm trying to restart a service with new settings when preferences change. The code I'm using is roughly similar to that from this question:

SettingsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initiate listener
    findPreference("service_switch").setOnPreferenceChangeListener(sRestartMyServiceOnChangeListener);

    ...

}

private Preference.OnPreferenceChangeListener sRestartMyServiceOnChangeListener = new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object obj) {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);

        // Stop service
        stopService(new Intent(SettingsActivity.this, MyService.class));

        // Restart service
        if (sharedPref.getBoolean("service_switch", false)) {
            startService(new Intent(SettingsActivity.this, MyService.class));
        }

        return true;
    }
};

This doesn't work on my phone, throwing the error:

findPreference(java.lang.CharSequence) is deprecated

This answer says that Android has moved to fragment-based activities. SettingsActivity is fragment-based... so I tried starting the listener in the fragment's onCreate():

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...

        // Initiate listener
        findPreference("service_switch").setOnPreferenceChangeListener(sRestartMyServiceOnChangeListener);

        ...

    }
}

Now I don't see the deprecation error, but I get a new one:

Non-static field 'sRestartMyServiceOnChangeListener' cannot be referenced from a static context.  

I can't make sRestartMyServiceOnChangeListener static, because stopService() and startService() are both non-static. I also can't make the fragment static:

This fragment inner class should be static

So how can I restart MyService from the preferences fragment when a setting changes?

Community
  • 1
  • 1
ki9
  • 5,183
  • 5
  • 37
  • 48
  • Why can't you make GeneralPreferenceFragment as non static? – siva Mar 05 '17 at 07:23
  • It throws the error `This fragment inner class should be static`. – ki9 Mar 05 '17 at 21:32
  • what is your outer class, what is the need for inner class? please post your complete code for better understanding. – siva Mar 06 '17 at 06:47
  • The outer class is the activity itself: `public class SettingsActivity extends AppCompatPreferenceActivity`. AFAIK, fragments must belong to an activity. There's some discussion about why fragment inner classes must be static in [this question](https://stackoverflow.com/questions/15571010/fragment-inner-class-should-be-static). – ki9 Mar 06 '17 at 06:58
  • then better to move the preference change listener to GeneralPreferenceFragment itself and invoke the service. get the context from activity and use it for invoking startservice method – siva Mar 06 '17 at 11:21

1 Answers1

0

It's a bit late but as I am doing research on that subject and still not finding any answer so I'm sharing my research:

I'm working on a workaround. Using this type of code directly in the XML:

<Preference
        android:summary="@string/summary_startBaseProfiling"
        android:title="@string/title_startBaseProfiling"
        android:key="settings_startBaseProfiling">
        <intent
    android:targetPackage="my.utar.phonesecurat"
    android:targetClass="my.utar.phonesecurat.BaseProfilingActivity" />

    </Preference>

I will launch a "Transitory activity" that will then start my service as usual, only problem is sending/sharing information that wanted to add as extra in your intent.

Hope it helped smh.

Good luck