Skip to content

Instantly share code, notes, and snippets.

@shobhitic
Last active January 28, 2020 19:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shobhitic/3eae4ee77d1eaf14296163b03b981ef8 to your computer and use it in GitHub Desktop.
Save shobhitic/3eae4ee77d1eaf14296163b03b981ef8 to your computer and use it in GitHub Desktop.
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<application>
<service
android:name="com.pilanites.streaks.SyncService"
android:exported="true"
android:process=":sync">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
<provider
android:name="com.activeandroid.content.ContentProvider"
android:authorities="@string/content_authority"
android:exported="false" />
</application>
public void sync() {
Account account = UserAccountUtil.getAccount(this);
Bundle settingsBundle = new Bundle();
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
/*
* Request the sync for the default account, authority, and
* manual sync settings
*/
ContentResolver.requestSync(account, getResources().getString(R.string.content_authority), settingsBundle);
}
public class SyncAdapter extends AbstractThreadedSyncAdapter {
private AccountManager mAccountManager;
public SyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
mAccountManager = AccountManager.get(context);
}
public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
super(context, autoInitialize, allowParallelSyncs);
mAccountManager = AccountManager.get(context);
}
@Override
public void onPerformSync(Account account, Bundle extras,
String authority, ContentProviderClient provider,
SyncResult syncResult) {
try {
String authToken = mAccountManager.blockingGetAuthToken(account,
AccountConstants.AUTH_TOKEN_TYPE, true);
// Use the authToken and write your sync logic. Skip the previous call if authToken is not required
} catch (OperationCanceledException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (AuthenticatorException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="@string/account_type"
android:allowParallelSyncs="true"
android:contentAuthority="@string/content_authority"
android:isAlwaysSyncable="true"
android:supportsUploading="true"
android:userVisible="false" />
/**
* Define a Service that returns an IBinder for the
* sync adapter class, allowing the sync adapter framework to call
* onPerformSync().
*/
public class SyncService extends Service {
// Storage for an instance of the sync adapter
private static SyncAdapter sSyncAdapter = null;
// Object to use as a thread-safe lock
private static final Object sSyncAdapterLock = new Object();
/*
* Instantiate the sync adapter object.
*/
@Override
public void onCreate() {
/*
* Create the sync adapter as a singleton.
* Set the sync adapter as syncable
* Disallow parallel syncs
*/
synchronized (sSyncAdapterLock) {
if (sSyncAdapter == null) {
sSyncAdapter = new SyncAdapter(getApplicationContext(), true);
}
}
}
/**
* Return an object that allows the system to invoke
* the sync adapter.
*/
@Override
public IBinder onBind(Intent intent) {
/*
* Get the object that allows external processes
* to call onPerformSync(). The object is created
* in the base class code when the SyncAdapter
* constructors call super()
*/
return sSyncAdapter.getSyncAdapterBinder();
}
}
// Use this method to get an instance of Account.
public static Account getAccount(Context context) {
if (ActivityCompat.checkSelfPermission(context,
android.Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "GET_ACCOUNTS not present.");
}
AccountManager accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccountsByType(AccountConstants.ACCOUNT_TYPE);
if (accounts.length > 0) {
return accounts[0];
} else {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment