本文整理汇总了Java中android.content.ContentResolver.addStatusChangeListener方法的典型用法代码示例。如果您正苦于以下问题:Java ContentResolver.addStatusChangeListener方法的具体用法?Java ContentResolver.addStatusChangeListener怎么用?Java ContentResolver.addStatusChangeListener使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.content.ContentResolver
的用法示例。
在下文中一共展示了ContentResolver.addStatusChangeListener方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onResume
import android.content.ContentResolver; //导入方法依赖的package包/类
@Override
protected void onResume() {
super.onResume();
// Perform one-time bootstrap setup, if needed
DataBootstrapService.startDataBootstrapIfNecessary(this);
// Check to ensure a Google Account is active for the app. Placing the check here ensures
// it is run again in the case where a Google Account wasn't present on the device and a
// picker had to be started.
if (!AccountUtils.enforceActiveGoogleAccount(this, SELECT_GOOGLE_ACCOUNT_RESULT)) {
LOGD(TAG, "EnforceActiveGoogleAccount returned false");
return;
}
// Watch for sync state changes
mSyncStatusObserver.onStatusChanged(0);
final int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING |
ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
mSyncObserverHandle = ContentResolver.addStatusChangeListener(mask, mSyncStatusObserver);
startLoginProcess();
}
示例2: setListening
import android.content.ContentResolver; //导入方法依赖的package包/类
@Override
public void setListening(boolean listening) {
if (listening && mEnabled) {
mSyncHandle = ContentResolver.addStatusChangeListener(
ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, mSyncObserver);
getSyncState();
if (DEBUG) log(getKey() + ": sync status listener registered");
} else if (mSyncHandle != null){
ContentResolver.removeStatusChangeListener(mSyncHandle);
mSyncHandle = null;
if (DEBUG) log(getKey() + ": sync status listener unregistered");
}
}
示例3: Google
import android.content.ContentResolver; //导入方法依赖的package包/类
/**
* Adds a Google account to the device.
*
* <p>TODO(adorokhine): Support adding accounts of other types with an optional 'type' kwarg.
*
* <p>TODO(adorokhine): Allow users to choose whether to enable/disable sync with a kwarg.
*
* @param username Username of the account to add (including @gmail.com).
* @param password Password of the account to add.
*/
@Rpc(
description = "Add a Google (GMail) account to the device, with account data sync disabled."
)
public void addAccount(String username, String password)
throws AccountSnippetException, AccountsException, IOException {
// Check for existing account. If we try to re-add an existing account, Android throws an
// exception that says "Account does not exist or not visible. Maybe change pwd?" which is
// a little hard to understand.
if (listAccounts().contains(username)) {
throw new AccountSnippetException(
"Account " + username + " already exists on the device");
}
Bundle addAccountOptions = new Bundle();
addAccountOptions.putString("username", username);
addAccountOptions.putString("password", password);
AccountManagerFuture<Bundle> future =
mAccountManager.addAccount(
GOOGLE_ACCOUNT_TYPE,
AUTH_TOKEN_TYPE,
null /* requiredFeatures */,
addAccountOptions,
null /* activity */,
null /* authCallback */,
null /* handler */);
Bundle result = future.getResult();
if (result.containsKey(AccountManager.KEY_ERROR_CODE)) {
throw new AccountSnippetException(
String.format(
Locale.US,
"Failed to add account due to code %d: %s",
result.getInt(AccountManager.KEY_ERROR_CODE),
result.getString(AccountManager.KEY_ERROR_MESSAGE)));
}
// Disable sync to avoid test flakiness as accounts fetch additional data.
// It takes a while for all sync adapters to be populated, so register for broadcasts when
// sync is starting and disable them there.
// NOTE: this listener is NOT unregistered because several sync requests for the new account
// will come in over time.
Account account = new Account(username, GOOGLE_ACCOUNT_TYPE);
Object handle =
ContentResolver.addStatusChangeListener(
ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE
| ContentResolver.SYNC_OBSERVER_TYPE_PENDING,
which -> {
for (SyncAdapterType adapter : ContentResolver.getSyncAdapterTypes()) {
// Ignore non-Google account types.
if (!adapter.accountType.equals(GOOGLE_ACCOUNT_TYPE)) {
continue;
}
// If a content provider is not whitelisted, then disable it.
// Because startSync and stopSync synchronously update the whitelist
// and sync settings, writelock both the whitelist check and the
// call to sync together.
mLock.writeLock().lock();
try {
if (!isAdapterWhitelisted(username, adapter.authority)) {
updateSync(account, adapter.authority, false /* sync */);
}
} finally {
mLock.writeLock().unlock();
}
}
});
mSyncStatusObserverHandles.add(handle);
}