I'm working on an application that will allow a user to turn on/off synching for their exchange email account.
I am able to get the exchange account using:
Account[] accs = AccountManager.get(this).getAccountsByType("com.htc.android.mail.eas");
but the Account API doesn't seem to offer what I'm looking for. I have also found ContactsContract.Settings database table which stores synch data, but I'm not that sure where to start with that.
Any ideas?
The ContentResolver contains the API you are looking for.
Check out the "setIsSyncable" method.
Related
I have inherited an app which comes as a free version, and Pro monthly subscription is bought via in-app items. The Pro status (a simple string "pro_status") is saved inside the AccountManager (package android.accounts).
//AccountManager initiated
AccountManager mAccountManager = AccountManager.get(this);
//fetch accounts which correspond to our package name
Account[] accounts = mAccountManager.getAccountsByType(getString(R.string.account_type));
//save Pro status inside the AccountManager
mAccountManager.setUserData(mAccount, "is_pro", "" + info.isPro());
The app suffers from a strange bug that it forgets the Pro state randomly, either when you close the app or during the work.
Is this a good way to store Pro status? Could AccountManager be the reason why the app loses the idea of a Pro status?
I usually use either a database or SharedPreferences to store such data (besides storing it on the remote API), so I need a help from someone who used his app in the same way.
Usually the people playing with the setting "pro" status will be the one with rooted devices. Anyways the data stored in the account manager is visible to such users.
So, for such a use case, even account manager is not safe. Though other apps (with different UID) cannot access this data.
Also keep in mind that the user can delete the account from the settings which might be the reason for your settings going away.
My advice would be save this info in shared pref in encrypted form??
In all phones,there is a specific User Database which stores information regarding your accounts.
Is this a good way to store Pro status?
I cannot answer that however I can give you answer to related questions
Is it modifiable?
There are 2 ways of accessing this data, viz
The user explicitly gives permission to an application to read
account details for that particular account. This list of apps
which can access the details for this account are stored based on
PIDs,which cannot be same for different apps. check setUserData
and AUTHENTICATE_ACCOUNTS permission
This database can be accessed(and modified ) on a rooted device.
Can a user manually delete this data?
-Yes,He can delete the account from the device itself.It is his device and he can modify any account details on it too.
In my experiece,the Acccount Manager API is very temperamental, and prone to change which can break your code if it is not used in the way that it was intended to be i.e. store User Account details in centralised database.
You should look into the approach and comments(but not the answer) of this question
.
I agree with the shared preferences approach too.
I'm developing an android application that Sync selected accounts at user defined frequencies.
AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
for(i=0;i<AcntCount;i++)
for (Account account : list)
if (emailPattern.matcher(account.name).matches())
if(account.name.matches(syncSettings.getString("Account"+i.toString(), null)))
//ContentResolver.addPeriodicSync(account, authority, extras, pollFrequency)
What are the values that should be given in place of authority and extras in the addPeriodicSync().
Do I have to write a sync-adapter for this?
Yes. You have to write a SyncAdapter. addPeriodicSync() is specifically used to tell ContentResolver to cause a SyncAdapter to fire on a timer.
What authority do you need? Well, SyncAdapters work on a (Account,Authority) pair.
The account specifies the specific login -- you might have multiple if you have more than one gmail address, for example.
The Authority tells the kind of data -- For example, with a gmail acocunt, you might be synchronizing... mail, contacts, calendar, finance, etc... So this tells you the type of data and the service that's bound to the account.
The pair together is provided to SyncAdapter -- More specifically you have a bunch of syncAdapters on the system (They define the authorities that are available to sync), and the correct syncadapter is looked up when PeriodicSync fires.
So if you're writing your own app that synchronizes against your own custom server, you'll write a SyncAdapter that knows how to read and store your schema, and you'll define an authority. These are just a string, and it's usually in java-classpath style. -- "com.example.SyncAdapterSample"
Read Why does ContentResolver.requestSync not trigger a sync? for all the steps you need to get a working SyncAdapter. It's definitely the way you want to go. A lot of steps to get it set up, but once the framework is there, it makes your life so much easier.
Now -- Extras. These are just some options for how the periodic sync will be done. You should be fine with the defaults, at first. Then dig into The documents. For example, the extra SYNC_EXTRAS_EXPEDITED indicates that this is a high priority update that should be synced right away -- usually, the scheduler waits a minute or two so that it can batch requests (and save battery, by not having the radio on all the time, but only in concentrated bursts). In general, you shouldn't need to worry about extras until your SyncAdapter gets much more complex than a simple baseline one.
I need to add new contacts dynamically from my application.
I list all the phone accounts with the AccountManager getAccounts method but I get accounts like weather, stock, etc.
How can I get only contact accounts ?
There's actually no good way to do this by using the API, the method we found to be most accurate was to think backwards: which accounts are used for storing contacts?
It's a bit crude, but it gives pretty good results (cache the list and you don't need to do it every time).
// Pseudo-code
for contact in RawContacts
uniqueAccountTypes.put(contact.account)
for accountType in uniqueAccountTypes
uniqueAccountsWithContacts.putAll(accountManager.getAccountsByType(accountType))
I don't have access a copy of the contacts database right now, so the code above might be slightly off, but the concept should be clear. Let me know if I should clarify further.
Had a similar situation.
After a little search found this method of AccountManager:
getAccountsByTypeAndFeatures();
here if you set parameter features as "service_mail" , "service_talk" etc, you will get accounts supporting e-mail and chat, hence these accounts will support contacts also.
UPDATE
"service_ah" will give the account device is linked to.
Is there a way to change the sync settings of a Gmail account programmatically with an Android app? For instance, I'd like to enable/disable syncing of a Gmail account from my app, without the user having to do anything.
I took a look at AccountManager, but that doesn't seem to be the right place to look.
You can change it through the API using ContentResolver.setSyncAutomatically(Account account, String authority, boolean sync). You need to get a handle to the account in question (usually through the AccountManager class...) and you'll need to lookup the content authority string. The latter you can get from android.Provider.xxxContract (ContactsContract for example...)
I can tell you:
Generally, sync is controlled via the BACKGROUND_DATA setting in Settings.Secure, which cannot be modified by applications
The Gmail application is not part of the SDK and so exposes no APIs
Under the 'Accounts & Sync' settings graphical OS menu, there are listings of user-configured Google accounts.
Please could someone point me in the right direction to programmatically change the sync settings associated with one of these accounts?
Thanks
Those settings use the new AccountManager APIs. Here's some sample code that shows how to add a new account. I'd assume that you'd want to get the credentials of one of the existing accounts and simply modify the data.
Unfortunately, I haven't had a chance to try out those APIs yet.
Maybe ContentResolver#setIsSyncable() can do what you want.
Perhaps would be better to fire the android.provider.Settings.ACCOUNT_SYNC_SETTINGS intent with either the EXTRA_AUTHORITIES extra set to ["google.com"], or possibly put the extra named "account" as the Account object you got from AccountManager. Then your user can turn their sync on or off for themselves.
ContentResolver.setSyncAutomatically(account, authority, true/false): sets whether or not the provider is synced when it receives a network tickle.