SyncAdapter Without an Account

我与影子孤独终老i 提交于 2019-12-03 00:23:25

In short the answer is: ContentProvider, AccountManager and SyncAdapter go together. You must have these three pieces, even if they are "dumb".

As stated above, "ContentProvider, AccountManager and SyncAdapter go together". For your application you can call the following activity the first time your app is loaded to authenticate and start synching automatically:

public class LoginActivity extends AccountAuthenticatorActivity {

private final static String DUMMY_ACCOUNT_NAME = "some_name";
private final static String DUMMY_ACCOUNT_PASS = "some_pass";
private final static String AUTHORITY = "com.android.contacts"; // for example

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Account account = new Account(DUMMY_ACCOUNT_NAME, Constants.ACCOUNT_TYPE);
    AccountManager am = AccountManager.get(this);
    if (am.addAccountExplicitly(account, DUMMY_ACCOUNT_PASS, null)) {
        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        setAccountAuthenticatorResult(result);
        ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
    }

    finish();
 }
}

This works in Android API 5+.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!