Do not save the selected email accountManager

0

How can I get the accountAuthenticator to save me the email that I indicated with the one I want to log in to the app?

This in my authenticator and service:

public class AccountAuthenticatorService extends Service {

private static final String LOG_TAG = AccountAuthenticatorService.class.getSimpleName();
private static AccountAuthenticatorImpl sAccountAuthenticator = null;

public AccountAuthenticatorService() {
    super();
}

@Override
public IBinder onBind(Intent intent) {
    IBinder ret = null;
    if (intent.getAction().equals(android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT)) {
        ret = getAuthenticator().getIBinder();
    }
    return ret;
}

public AbstractAccountAuthenticator getAuthenticator() {
    if (AccountAuthenticatorService.sAccountAuthenticator == null) {
        AccountAuthenticatorService.sAccountAuthenticator = new AccountAuthenticatorImpl(this);
    }
    return AccountAuthenticatorService.sAccountAuthenticator;
}

private static class AccountAuthenticatorImpl extends AbstractAccountAuthenticator {
    private Context mContext;

    public AccountAuthenticatorImpl(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
        return null;
    }

    // When adding a new account from the cell phone, this window opens
    @Override
    public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
        Intent add = new Intent(mContext, AccountManagerSimpleActivity.class);
        add.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
        Bundle reply = new Bundle();
        reply.putParcelable(AccountManager.KEY_INTENT, add);
        return reply;
    }

    @Override
    public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
        return null;
    }

    @Override
    public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
                    AccountManager am = AccountManager.get(mContext);
        String user = account.name;
        String pass = am.getPassword(account);
        String token = "sample-no-token";

        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, user);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, "com.google");
        result.putString(AccountManager.KEY_AUTHTOKEN, token);
        return result;
    }

    @Override
    public String getAuthTokenLabel(String authTokenType) {
        return null;
    }

    @Override
    public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
        return null;
    }

    @Override
    public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
        return null;
    }

}
}

If I want to register, the window indicated in the addAcount opens. The main record class:

  // Button to execute the login from gmail account
    registration.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = "com.google";
            try{
                Intent googlePicker = AccountManager.newChooseAccountIntent(null, null,
                        new String[]{email}, true, null, null, null, null);
                startActivityForResult(googlePicker, PICK_ACCOUNT_REQUEST);
            } catch (ActivityNotFoundException ex) {
                Toast.makeText(AccountManagerSimpleActivity.this, "Error de sincronización en las cuentas: " + ex.getMessage(), Toast.LENGTH_LONG);
            }
        }
    });

    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

}

// Function that complements the registry by gmail
@SuppressLint("WrongConstant")
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    if (requestCode == PICK_ACCOUNT_REQUEST && resultCode == RESULT_OK) {
        String useremail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        Toast.makeText(this,"Cuenta sincronizada con éxito",3000).show();
    }else{
        Toast.makeText(this,"No hay cuentas sincronizadas",3000).show();

    }
}

This window returns the emails added to my mobile, but if I select one of the ones I have, it does not save it as a known account so that when I start the app it starts automatically with that email.

Added methods in the main.

On the onCreate: mAccounts = mAccountManager.getAccounts ();

    // Returns all registered accounts in the phone and executes the selected account
private void showAccountListDialog() {
    List<String> list = new ArrayList<String>();
    for (Account account : mAccounts) {
        list.add(account.name);
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            mAccountManager.getAuthToken(mAccounts[which], "jp.example", null, mActivity, mCallback, null);
        }
    });
    builder.show();
}

The asynchronous method to open the window when opening the app:

    // Connection with the php server asynchronous
public void connSRV_async(String users, String passw) {
    try {
            listener = new XMLRPCCallback() {
                @Override
                public void onResponse(long id, Object result) {
                    // Handling the servers response
                    if(iConexion==id) {
                        code = String.valueOf(result);
                        //aproved_loguser();
                        mCallback = new AccountManagerCallback<Bundle>() {
                            @Override
                            public void run(AccountManagerFuture<Bundle> future) {
                                aproved_loguser();
                            }
                        };
                    }else{
                        Log.e("ID","Se han producido varios errores");
                    }
                }

I have managed to let me create an account and add it, but I do not know why when starting the app it does not start automatically ...

    
asked by Pedro 08.05.2018 в 10:38
source

0 answers