下面列出了android.content.ContentResolver#isSyncActive ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean cancelPendingActiveSync(Account mChosenAccount) {
boolean pending = ContentResolver.isSyncPending(mChosenAccount, Contract.AUTHORITY);
if (pending) {
LogUtil.log(TAG, "Warning: sync is PENDING. Will cancel.");
}
boolean active = ContentResolver.isSyncActive(mChosenAccount, Contract.AUTHORITY);
if (active) {
LogUtil.log(TAG, "Warning: sync is ACTIVE. Will cancel.");
}
if (pending || active) {
LogUtil.log(TAG, "Cancelling previously pending/active sync.");
ContentResolver.cancelSync(mChosenAccount, Contract.AUTHORITY);
return true;
}
return false;
}
public boolean requestSync(boolean onlyCheckins) {
if (!isUserLoggedIn()) {
return false;
}
Bundle extras = new Bundle();
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
extras.putString(SyncAdapter.EXTRA_AUTH_TOKEN, mAuthToken);
extras.putBoolean(SyncAdapter.EXTRA_ONLY_CHECKINS, onlyCheckins);
ContentResolver.setSyncAutomatically(mAccount, Table.AUTHORITY, true);
ContentResolver.setIsSyncable(mAccount, Table.AUTHORITY, 1);
if (ContentResolver.isSyncPending(mAccount, Table.AUTHORITY) ||
ContentResolver.isSyncActive(mAccount, Table.AUTHORITY)) {
ContentResolver.cancelSync(mAccount, Table.AUTHORITY);
}
ContentResolver.requestSync(mAccount, Table.AUTHORITY, extras);
return true;
}
private void showSyncIndicator(){
AccountManager am = AccountManager.get(getActivity());
Account account = am.getAccountsByType(getActivity().getString(R.string.ACCOUNT_TYPE))[0];
final boolean isSyncing = (ContentResolver.isSyncActive(account, "com.android.contacts") || ContentResolver.isSyncPending(account, "com.android.contacts"));
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (isSyncing) {
if (((LinearLayout) getView()).getChildCount() == 1)
((LinearLayout) getView()).addView(LayoutInflater.from(getActivity()).inflate(R.layout.contact_loading_indicator, null), 0);
} else{
if (((LinearLayout) getView()).getChildCount() > 1)
((LinearLayout) getView()).removeViewAt(0);
}
}
});
}
/**
* Helper method to trigger an immediate sync ("refreshAccounts").
* <p>
* <p>This should only be used when we need to preempt the normal sync schedule. Typically, this
* means the user has pressed the "refreshAccounts" button.
* <p>
* Note that SYNC_EXTRAS_MANUAL will cause an immediate sync, without any optimization to
* preserve battery life. If you know new data is available (perhaps via a GCM notification),
* but the user is not actively waiting for that data, you should omit this flag; this will give
* the OS additional freedom in scheduling your sync request.
*/
public void triggerRefresh(MovirtAccount account) {
Account acc = account.getAccount();
Bundle b = new Bundle();
// Disable sync backoff and ignore sync preferences. In other words...perform sync NOW!
b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
// cancel sync because account will not sync if similar sync is already in progress
if (ContentResolver.isSyncPending(acc, OVirtContract.CONTENT_AUTHORITY) ||
ContentResolver.isSyncActive(acc, OVirtContract.CONTENT_AUTHORITY)) {
ContentResolver.cancelSync(acc, OVirtContract.CONTENT_AUTHORITY);
}
ContentResolver.requestSync(acc, OVirtContract.CONTENT_AUTHORITY, b);
}
/**
* Returns true if sync is active.
*
* @param context the context
*/
public static boolean isSyncActive(Context context) {
Account[] accounts = AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE);
for (Account account : accounts) {
if (ContentResolver.isSyncActive(account, SYNC_AUTHORITY)) {
return true;
}
}
return false;
}
public void setAutoSync(String authority, boolean autoSync) {
try {
Account account = mUser.getAccount();
if (!ContentResolver.isSyncActive(account, authority)) {
ContentResolver.setSyncAutomatically(account, authority, autoSync);
}
} catch (NullPointerException e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
}
public void refreshFinished(Account selectedAccount){
if(selectedAccount == null || !ContentResolver.isSyncActive(selectedAccount,MainActivity.AUTHORITY)){
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
reloadData();
mSwipeRefreshLayout.setRefreshing(false);
}
});
}
}
public void setAutoSync(String authority, boolean autoSync) {
try {
Account account = mUser.getAccount();
if (!ContentResolver.isSyncActive(account, authority)) {
ContentResolver.setSyncAutomatically(account, authority, autoSync);
}
} catch (NullPointerException e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
}
public boolean isSyncActive() {
return ContentResolver.isSyncActive(mAccount, HackerNewsData.CONTENT_AUTHORITY);
}
public static void requestManualSync(Context context, Bundle args) {
Account account = AccountUtils.getActiveAccount(context);
if (account != null) {
LOGD(TAG, "Requesting manual sync for account " + account.name
+" args=" + args.toString());
args.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
args.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
args.putBoolean(SyncAdapter.EXTRA_SYNC_REMOTE, false);
AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
accountManager.addAccountExplicitly(account, null, null);
// Inform the system that this account is eligible for auto sync when the network is up
ContentResolver.setSyncAutomatically(account, V2exContract.CONTENT_AUTHORITY, true);
// Inform the system that this account supports sync
ContentResolver.setIsSyncable(account, V2exContract.CONTENT_AUTHORITY, 1);
boolean pending = ContentResolver.isSyncPending(account,
V2exContract.CONTENT_AUTHORITY);
if (pending) {
LOGD(TAG, "Warning: sync is PENDING. Will cancel.");
}
boolean active = ContentResolver.isSyncActive(account,
V2exContract.CONTENT_AUTHORITY);
if (active) {
LOGD(TAG, "Warning: sync is ACTIVE. Will cancel.");
}
if (pending || active) {
LOGD(TAG, "Cancelling previously pending/active sync.");
ContentResolver.cancelSync(account, V2exContract.CONTENT_AUTHORITY);
}
LOGD(TAG, "Requesting sync now.");
ContentResolver.requestSync(account, V2exContract.CONTENT_AUTHORITY, args);
} else {
LOGD(TAG, "Can't request manual sync -- no chosen account.");
}
}