In an attempt to write a custom Android sync adapter I followed this. I was success at showing an entry (Account settings) in General setting with the following code snippet from above said example.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General Settings" />
<PreferenceScreen android:key="account_settings"
android:title="Account Settings" android:summary="Sync frequency, notifications, etc.">
<intent android:action="fm.last.android.activity.Preferences.ACCOUNT_SETUP"
android:targetPackage="fm.last.android"
android:targetClass="fm.last.android.activity.Preferences" />
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
The code resulted me an entry (Account Settings) in General settings:
Upon clicking the Account Settings I'm getting an error as follows and the device reboots unnecessarily.
ERROR/AndroidRuntime(30057): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I know this error can be solved through code. Since "Account Settings" preference is XML-based code I'm stuck with the error.
Can anyone help to solve the Issue?
How do we control these kind of preferences through code?
I won't exactly answer your 2 questions, but I addressed this problem by working around it using the following 3 steps:
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.)
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With