Android Open Source - AndroidSyncAndAccounts Accounts Activity






From Project

Back to project page AndroidSyncAndAccounts.

License

The source code is released under:

Apache License

If you think the Android project AndroidSyncAndAccounts listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.jakubczyk.syncandaccoutns.provider;
//from w w w  . j av  a2 s  .  c o m
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import org.jakubczyk.syncandaccoutns.lib.AccountHelper;


public class AccountsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accoutns);

        TextView textView = (TextView) findViewById(R.id.accounts_tv);

        AccountHelper.createAccount(this);

        final Account myAccount = AccountHelper.getMyAccount(this);

        textView.setText(String.format("%s\nPassword: %s", myAccount.toString(), AccountManager.get(this).getPassword(myAccount)));


        // FORCE SYNC
        final Bundle forcedSyncBundle = new Bundle();
        // The two flags are required to schedule forced sync
        forcedSyncBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        forcedSyncBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        forcedSyncBundle.putString("MANUAL", "This Sync was triggered by manual action!");


        findViewById(R.id.sync_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentResolver.requestSync(myAccount, getString(R.string.authority), forcedSyncBundle);
            }
        });


        // PERIODIC SYNC

        // This is also handled in AndroidManifest.xml
        ContentResolver.setIsSyncable(myAccount, getString(R.string.authority), 1);
        ContentResolver.setSyncAutomatically(myAccount, getString(R.string.authority), true);


        // Create own bundle which is passed to Sync Service Adapter. Here you can put whatever you want.
        Bundle periodicBundle = new Bundle();
        periodicBundle.putString("forced by", "time");
        periodicBundle.putString("PERIODIC", "Yes ! It's periodic sync triggered by OS");
        ContentResolver.removePeriodicSync(myAccount, getString(R.string.authority), periodicBundle);
        ContentResolver.addPeriodicSync(myAccount, getString(R.string.authority), periodicBundle, 30);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.accoutns, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}




Java Source Code List

org.jakubczyk.syncandaccoutns.foreignreader.ForeignActivity.java
org.jakubczyk.syncandaccoutns.friendly.FriendlyActivity.java
org.jakubczyk.syncandaccoutns.lib.AccountHelper.java
org.jakubczyk.syncandaccoutns.provider.AccountsActivity.java
org.jakubczyk.syncandaccoutns.provider.AuthenticatorService.java
org.jakubczyk.syncandaccoutns.provider.Authenticator.java
org.jakubczyk.syncandaccoutns.provider.StubProvider.java
org.jakubczyk.syncandaccoutns.provider.SyncAdapter.java
org.jakubczyk.syncandaccoutns.provider.SyncService.java