Check if Sync is Enabled within Android App - android

Is there a way to check, programmatically within my Android app, whether a particular setting under Settings > Accounts and Sync > Data & Synchronization is enabled or not?
Is there a way to check if the general sync settings are enabled?
Thanks!
If it helps to know "why," I'm currently rolling my own sync functionality (not using SyncAdapter). However, if possible I'd like to have my sync service listed under Data & Synchronization. Right now I'm planning to hack a dummy sync service that does nothing and have my app's sync service query whether or not the dummy sync service is enabled. That will tell me whether to sync or not.

To know if a sync is enabled (and not active as rajpara's answer do), use this:
AccountManager am = AccountManager.get(YourActivity.this);
Account account = am.getAccountsByType(YOUR_ACCOUNT_TYPE)[0];
boolean isYourAccountSyncEnabled = ContentResolver.getSyncAutomatically(account, DataProvider.AUTHORITY);
boolean isMasterSyncEnabled = ContentResolver.getMasterSyncAutomatically();
The "master" sync status is the global sync switch the user can use to disable all sync on his phone. If the master sync is off, your account won't sync, even if your account sync status tells that it's enabled.
As #HiB mentionned, the android.permission.READ_SYNC_SETTINGS permission is needed to access the sync status. android.permission.WRITE_SYNC_SETTINGS is needed to enable/disable it.
You also need android.permission.GET_ACCOUNTS to get the accounts, as MeetM mentionned.

You can check whether sync is enable or not with the help of below code and this Document
AccountManager am = AccountManager.get(YourActivity.this);
Account account = am.getAccountsByType(Const.ACCOUNT_TYPE)[0];
if(ContentResolver.isSyncActive(account, DataProvider.AUTHORITY){
// sync is enable
}
You can also set enable/disable programmatically with the help of this ContentResolver.setSyncAutomatically and ContentResolver.setMasterSyncAutomatically
Update :
isSyncActive returns true if there is currently a sync operation for the given account or authority in the pending list, or actively being processed.

boolean isEnabled = ContentResolver.getSyncAutomatically(account, MyProvider);
if(isEnabled)
{
...do something
}
Works for me

Related

Android: SyncAdapter how do I start the auto sync

I have the auto sync running if I select the checkbox through the Setting/Account screen. If I set the UserVisible in the syncadapter.xml to false, I can't select the check box. How to start the auto sync without the option in setting screen.
To manually invoke a sync you call RequestSync with at least the Account and Authority string that you have already created for your SyncAdapter.
ContentResolver.RequestSync(account, authority, null);
That will have OS start your registered sync Service and thus your SyncAdapter.
Re: https://developer.android.com/reference/android/content/ContentResolver.html#requestSync(android.accounts.Account,%20java.lang.String,%20android.os.Bundle)

Android AccessibilityManager getEnabledAccessibilityServiceList returns empty list under certain circumstances

My app extends Android's AccessibilityService to monitor the currently active app. In order to detect changes in activities I have to register my service and get the user to consent to allow the permission. As a convenience to the user I detect whether my service is currently enabled when my app starts, if not enabled e.g. when the app is installed then I redirect the user to the Settings Accessibility page to allow them to enable the service. The following code checks whether my service is currently enabled, the id parameter is the id of my service e.g. com.foo.bar/.MyService syntax:
private boolean isAccessibilityEnabled(String id) {
AccessibilityManager am = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
List<AccessibilityServiceInfo> enabledServices = am.getEnabledAccessibilityServiceList(AccessibilityEvent.TYPES_ALL_MASK);
for (AccessibilityServiceInfo service : enabledServices) {
if (id.equals(service.getId())) {
return true;
}
}
return false;
}
I've seen two scenarios where this check fails even though I know the service is enabled. Firstly if I debug my app from Android Studio this fails and enabledServices is empty, if I run (rather than debug) it works perfectly and returns a single entry in enabledServices. The second scenario I've noticed where it fails is when I run the adb backup command to backup my app, if my app is currently visible when the backup prompt comes up then when the backup completes my app's main activity onCreate is executed and the isAccessibilityEnabled method is checked and again fails to see my service is already enabled.
Is there a special case I need to take into account when calling getEnabledAccessibilityServiceList or is there a reason why my service isn't returned even though when I look in the Accessibility Settings page I can see the service is enabled.
There is an alternative way to query if an accessibility service is enabled, I've used the approach described here - Detect if my accessibility service is enabled and it seems to get around the issues I was encountering.

Why is ContentResolver.setIsSyncable not working for me?

I am writing an app which uses the SyncAdapter framework. By default, the user has to activate the sync in the android account settings.
However, I was told that by calling
ContentResolver.setIsSyncable (mAccount, AUTHORITY,1);
I can activate the syncing without any user interaction.
Sadly, this doesn't seem to work. Even after a complete reinstall of the app, the sync adapter service is not running and the account settings look like this:(See screenshot below.)
What could I possibly be missing? Is it a problem maybe that I am calling ContentResolver.setIsSyncable (mAccount, AUTHORITY,1); from the content provider as opposed to the activity?

Toggle on auto-sync progarammatically in Android

May be this question is asked about 2-4 year ago.Still m not satisfied.My problem is: "how to turn on/off auto Sync Programmatically".I dont want any specific data to synchronous.Just want to know Is that possible to enable or disable Auto-sync programmatically ? If possible, then How? Can anyone give me Example?
which is shown like this on Android Screen:
What is the use of ContentResolver.setSyncAutomatically(account, authority, true); in auto-sync?
Thanks in Advance.
I guess you are talking about gmail auto sync the email right ? One way is setMasterSyncAutomatically() (ContentResolver), applies to all the accounts (and providers). If you set this off/false you can disable all the syncs.
This is what we have from android documentation.
public static void setMasterSyncAutomatically (boolean sync)
Added in API level 5
Sets the master auto-sync setting that applies to all the providers and accounts. If this is false then the per-provider auto-sync setting is ignored.
This method requires the caller to hold the permission WRITE_SYNC_SETTINGS.
Parameters
sync the master auto-sync setting that applies to all the providers and accounts

Android: Enable/Disable Auto Sync and Background Data

I want to develop an application that disables the Background Data (new feature in Android 1.5) and Auto Sync and then enables GPRS/EDGE connection and vice versa.
I figured out how to enable/disable GPRS/EDGE by changing the APN settings. (weird solution. However; Android developers couldn't think a user may want to disable GPRS/EDGE) But, I couldn't find a way to enable/disable Auto Sync and Background data.
I investigated the Android code and as I understood, the Sync operation is an intent. So, I wanted to reach with putExtra to the intent and trigger the enabling/disabling. But; I couldn't find the correct keyword. Or maybe I was totally wrong.
What is the right way to solve this?
In my HTC dreams, there is a checkbox to disable the auto sync. I can look for the menu arborescence if you wish so you can find what the callback function is in the Android source code. But I am pretty sure auto sync cannot be completely disabled. Unchecking auto sync will prevent sync from being performed on a timed basis, but it will occur everytime you run an app with sync capabilities if any network data connection is available.
Good luck anyway.
EDIT :
There are two ways to get the info you desire.
First, I think you can use the code in android-sources/packages/apps/Settings/src/com/android/settings/Utils.java to create an activity that will enlist all the keys of the intent then find the one you want.
The other way is to write a nice mail to the guy who made the Toggle Setting app (http://smartphoneandroid.com/2008/12/28/toggle-setting-perfect-app-for-android-phone.html) since he obviously found a solution to your problem. His email address is written in the app sheet on the android market. I won't write it here, but if you do not have access to real android phone, I can mail it to you on your mail address.
Background data is a secure setting, so cannot be changed by user applications. But bear in mind, it's just a setting - it's not enforced. Apps are meant to read it and respect it but I bet some don't.
To Disable the AutoSynch
ContentResolver.setMasterSyncAutomatically(false);
To Enable the AutoSynch
ContentResolver.setMasterSyncAutomatically(true);
Permission you require is
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
setMasterSyncAutomatically() on ContentResolver should do it. Check: general-sync-settings-auto-sync-checkbox-programtically

Categories

Resources