SyncAdapter Without an Account

前端 未结 2 2032
耶瑟儿~
耶瑟儿~ 2021-02-04 16:03

I\'m trying to create a SyncAdapter for my Android app to show YouTube videos from one specific channel. The videos are public domain so I don\'t want the user to login, create

相关标签:
2条回答
  • 2021-02-04 16:33

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

    0 讨论(0)
  • 2021-02-04 16:39

    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+.

    0 讨论(0)
提交回复
热议问题