How do we control an Android sync adapter preference?

穿精又带淫゛_ 提交于 2019-12-03 00:29:43

I won't exactly answer your 2 questions, but I addressed this problem by working around it using the following 3 steps:

  1. Set up Account Preferences XML
  2. Create an activity to manage preferences
  3. Extract the account information from the "preference editing" Intent

Setting up the account preferences XML

I used an account_preferences.xml very similar to the one in the SDK sample and the c99 Last.fm application. Consider the following snippet:

<PreferenceScreen
          android:key="account_settings"
          android:title="Account Preferences"
          android:summary="Misc account preferences">
          <intent
              android:action="some.unique.action.name.account.EDIT"
              android:targetPackage="com.example.preferences"
              android:targetClass="com.example.preferences.PreferencesActivity">
          </intent>
</PreferenceScreen>

Given this, here are some of the important points I've found: (Note that I've found these through experimentation and not through any specific Android documentation -- if future readers of this question have those references, it'd be great to link those in.)

  • The android:key for this PreferenceScreen must be "account_settings" or Android will not find & display your preferences
  • By using an explicit Intent and specifying the targetPackage and targetClass, android will start your Activity directly and you don't need to worry about an Intent filter.
  • Android stores the Account object for the currently selected account in this Intent's Extras -- which is very important on the receiving end so you can know which account you're managing. More on this below.

Creating the preference managing Activity

Next I created an Activity to correspond to the package and class specified in the above XML. Note that as far as I can tell, the choice of Activity is up to you -- it's most common to subclass android.preference.PreferenceActivity but I've also subclassed Activity directly. Standard Activity development guidelines apply here...

Getting the Account from the "preference editing" Intent

When your Activity starts up, you can extract the corresponding Account object from the Extras Bundle (using this.getIntent().getExtras()) and the key "account". Recall that this Intent will be the one that you specified in preferences XML file initially. (Again, I could not find doc on this so found it by dumping the contents of the Extras Bundle passed in with my Intent.) Once you have the Account, it should be straightforward to load/save preferences for that account using SharedPreferences, your database, or whatever other method you prefer.

Hope that helps...

The files/resources refered to above in the are not in the stand alone package : this is the only thing the author forgot to adapt i guess : you have to create your own preference class . here is my class :

public class AccountPreferences extends PreferenceActivity {
public static final String TAG = "AccountPreferences";
private boolean shouldForceSync = false;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    Log.i(TAG, "onCreate");
    addPreferencesFromResource(R.xml.preferences_resources);

@Override
public void onPause() {
    super.onPause();
    if (shouldForceSync) {
        AccountAuthenticatorService.resyncAccount(this);
    }
}

Preference.OnPreferenceChangeListener syncToggle = new Preference.OnPreferenceChangeListener() {
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        shouldForceSync = true;
        return true;
    }
};

and here is the preferences file : preferences_resources.xml

    <PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/privacy_preferences">

    <CheckBoxPreference
        android:key="privacy_contacts"
        android:defaultValue="true"
        android:summary="@string/privacy_contacts_summary" android:title="@string/privacy_contacts_title"/>
</PreferenceCategory>

<PreferenceCategory android:title="@string/outgoing_preferences">

    <CheckBoxPreference
        android:key="allow_mail"
        android:defaultValue="true"
        android:summary="@string/allow_mail" android:title="@string/allow_mail_text"/>

</PreferenceCategory>

you will have to adapt those, or have a deeper look at the files in his last.fm project.

hope this helps, good luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!