I want to read in my code preferences I have set via the authenticator xml file. I found Can't access preferences set in account-authenticator in Android and How can access preferences set in account-authenticator in Android one is completely unanswered and the other says I need to create my own activity. This really sounds odd since that would mean that the preferences I can configure via xml are useless because I never can read them again. That cannot be. Does someone know more about it? If I really have to create an own activity, how would I do this in the case of the authenticator?
1 Answers
From the documentation for AbstractAccountAuthenticator:
The preferences attribute points to a PreferenceScreen xml hierarchy that contains a list of PreferenceScreens that can be invoked to manage the authenticator. An example is:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/title_fmt"/>
<PreferenceScreen
android:key="key1"
android:title="@string/key1_action"
android:summary="@string/key1_summary">
<intent
android:action="key1.ACTION"
android:targetPackage="key1.package"
android:targetClass="key1.class"/>
</PreferenceScreen>
</PreferenceScreen>
So it appears that even though it is possible to put individual preferences in account_preferences.xml
it is not intended to be done so the values are not accessible.
See this question and answer for details about how to setup and handle the PreferenceScreen intent.
EDIT
For a very basic working example, you can download the sample app from the Sync Adapter Training Docs and edit as follows.
Create res/xml/account_preferences.xml
that looks like this
<?xml version="1.0" encoding="UTF-8" ?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Category"/>
<PreferenceScreen
android:key="key1"
android:title="@string/app_name"
android:summary="@string/account_name">
<intent
android:targetPackage="com.example.android.network.sync.basicsyncadapter"
android:targetClass="com.example.android.network.sync.basicsyncadapter.EntryListActivity"/>
</PreferenceScreen>
</PreferenceScreen>
Add android:accountPreferences="@xml/account_preferences"
to the account-authenticator
tag in authenticator.xml
.
This example starts up an existing activity in the example, but could easily start a PreferenceActivity (or any other activity you want). See the Settings guide for details about how to set up the PreferenceActivity.
For a real world example from a core Android app, see the implementation of the Email app here.
-
It can be done but it is not intended sounds already odd. Another prove is that if you look into various accounts, e.g. Google account, GMX-Mail account, and others - just in case Google Account doesn't count. They all use these settings. The intent does NOT work in the authenticator, it works in normal apps like in the link you gave. In an authenticator it doesn't work. As soon as the intent tag is there the preference doesn't show up. Did you get it ever to work in an authenticator and _not_ in a normal app? – AndyAndroid Jul 02 '15 at 07:33
-
I've updated my answer showing you how to edit one of the Android sample apps so that it starts an activity from the account screen. I have also included a link to the core Android email application which shows a similar implementation. – mpkuth Jul 02 '15 at 13:03
-
If there is no better answer coming I will award you the bounty, but your answer is not 100% correct. But it lead me to a way to work around the issue. Maybe it is a device / API level specific problem. I looked as well into the code of davdroid. For other running into that issue: 1. try to export=true your pref activity, some claim it is necessary, for davdroid it doesn't seem. 2. There is a bug (?) that you _must_ have an action in your intent. 3. For me, again davdroid seems to work, I had to remove in the intent targetPackage and targetClass, only action. – AndyAndroid Jul 03 '15 at 07:13
-
So all in all the situation why other apps behave differently is not too clear yet. With the above work arounds it should be possible to get it working to start an own activity. If someone knows more please add more answers! – AndyAndroid Jul 03 '15 at 07:17
-
@mpkuth thank u man. that email app has a lot to learn from ;) – Harkal Aug 07 '19 at 02:01