1
+ package net .yuki24 .examples .AccountManager ;
2
+
3
+ import java .util .ArrayList ;
4
+
5
+ import net .yuki24 .examples .AccountManager .authenticator .ExampleAccountAuthenticator ;
6
+
7
+ import android .accounts .Account ;
8
+ import android .accounts .AccountAuthenticatorActivity ;
9
+ import android .accounts .AccountManager ;
10
+ import android .app .Activity ;
11
+ import android .content .Intent ;
12
+ import android .os .Bundle ;
13
+ import android .provider .SyncStateContract .Constants ;
14
+
15
+ public class AccountManagerExampleActivity extends AccountAuthenticatorActivity {
16
+ @ Override
17
+ public void onCreate (Bundle savedInstanceState ) {
18
+ super .onCreate (savedInstanceState );
19
+ setContentView (R .layout .main );
20
+
21
+ ArrayList <String > accountsInfo = new ArrayList <String >();
22
+ AccountManager manager = AccountManager .get (this );
23
+ Account [] accounts = manager .getAccounts ();
24
+ for (Account account : accounts ) {
25
+ String name = account .name ;
26
+ String type = account .type ;
27
+ int describeContents = account .describeContents ();
28
+ int hashCode = account .hashCode ();
29
+
30
+ accountsInfo .add ("name = " + name +
31
+ "\n type = " + type +
32
+ "\n describeContents = " + describeContents +
33
+ "\n hashCode = " + hashCode );
34
+ }
35
+ String [] result = new String [accountsInfo .size ()];
36
+ accountsInfo .toArray (result );
37
+
38
+ String username = "hoge" ;
39
+ String password = "fuga" ;
40
+ String accountType = "net.yuki24.examples" ;
41
+
42
+ // This is the magic that addes the account to the Android Account Manager
43
+ final Account account = new Account (username , accountType );
44
+ manager .addAccountExplicitly (account , password , null );
45
+
46
+ // Now we tell our caller, could be the Andreid Account Manager or even our own application
47
+ // that the process was successful
48
+ final Intent intent = new Intent ();
49
+ intent .putExtra (AccountManager .KEY_ACCOUNT_NAME , username );
50
+ intent .putExtra (AccountManager .KEY_ACCOUNT_TYPE , accountType );
51
+ intent .putExtra (AccountManager .KEY_AUTHTOKEN , accountType );
52
+ this .setAccountAuthenticatorResult (intent .getExtras ());
53
+ this .setResult (RESULT_OK , intent );
54
+ this .finish ();
55
+ }
56
+ }
0 commit comments