SyncAdapter Without an Account

前端 未结 2 2033
耶瑟儿~
耶瑟儿~ 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: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+.

提交回复
热议问题