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?
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 :
and here is the preferences file : preferences_resources.xml
you will have to adapt those, or have a deeper look at the files in his last.fm project.
hope this helps, good luck.
I won't exactly answer your 2 questions, but I addressed this problem by working around it using the following 3 steps:
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:
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.)
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...