本文整理匯總了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);
}