2010年9月23日木曜日

Android 端末に設定されているアカウント情報を取得

端末に設定されているアカウントの一覧を取得するには

 AccountManager

を使います。(API Level 5)

AndroidManifest.xml に

 <uses-permission android:name="android.permission.GET_ACCOUNTS" />

を書くのを忘れずに。

 AccountManager.get(this) で現在の Context と関連付いている
 AccountManager のインスタンスが取得できます。
 このインスタンスに対して、getAccounts()
 getAccountsByType() で Account の配列が取得できます。
 Account クラスはアカウント名とタイプを保持しています。


アカウント名や名前を取得するには、こんなかんじ。
  1. private String[] getAccounts() {  
  2.     ArrayList<string> accountsInfo = new ArrayList<string>();  
  3.     Account[] accounts = AccountManager.get(this).getAccounts();  
  4.     for (Account account : accounts) {  
  5.         String name = account.name;  
  6.         String type = account.type;  
  7.         int describeContents = account.describeContents();  
  8.         int hashCode = account.hashCode();  
  9.      
  10.         accountsInfo.add("name = " + name +   
  11.                          "\ntype = " + type +   
  12.                          "\ndescribeContents = " + describeContents +   
  13.                          "\nhashCode = " + hashCode);  
  14.     }  
  15.   
  16.     String[] result = new String[accountsInfo.size()];  
  17.     accountsInfo.toArray(result);  
  18.     return result;  
  19. }  
  20. </string></string>  




 

0 件のコメント:

コメントを投稿