我是Android开发的新手,很抱歉,如果我问一个非常明显的问题。对于我的应用程序,我想使用客户经理自动添加一个谷歌帐户。我是否想要使用addAccount或addAccountExplicitly,如果帐户名称为“platinum”且密码为“software”,我该如何解决此问题。
答案 0 :(得分:13)
试试这个:
需要许可:
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/></uses-permission>
<uses-permission android:name="android.permission.ACCOUNT_MANAGER"></uses-permission>
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission>
<强> AuthenticatorActivity.java 强>
public class SleepyAccountAuthenticatorActivity extends AccountAuthenticatorActivity
{
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.new_account);
final Button done = (Button) findViewById(R.id.new_account_done);
final EditText server = (EditText) findViewById(R.id.new_account_server);
final EditText username = (EditText) findViewById(R.id.new_account_username);
final EditText password = (EditText) findViewById(R.id.new_account_password);
final Activity self = this;
done.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//Account
Account account=new Account(username.getText().
toString(), getString(R.string.ACCOUNT_TYPE));
Bundle userdata = new Bundle();
userdata.putString("SERVER", server.getText().toString());
//AccountManager
AccountManager am = AccountManager.get(self);
if (am.addAccountExplicitly(account, password.
getText().toString(), userdata))
{
Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, username.getText().toString());
result.putString(AccountManager.KEY_ACCOUNT_TYPE,getString(R.string.ACCOUNT_TYPE));
setAccountAuthenticatorResult(result);
}
finish();
}
});
}
}
\ res \ xml \ authenticator.xml
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.yarin.AccountType"
android:icon="@drawable/icon"
android:smallIcon="@drawable/icon"
android:label="@string/ACCOUNT_LABEL"
android:accountPreferences="@xml/account_preferences"
/>