使用這個類別必須加入下方的權限,加了之後可以透過AccountManager類別去取得使用者的資訊,利用資訊再去篩選信箱帳號等等的。
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Activity程式碼如下:
package com.example.getacc;
import java.util.ArrayList;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button button;
AccountManager accountManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
accountManager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(getAcc);
}
Button.OnClickListener getAcc = new Button.OnClickListener() {
public void onClick(View v) {
Account[] account = accountManager.getAccounts();
for (Account acc : account) {
if (acc.name.indexOf("@") != -1) {
Log.e("Mail:", acc.name);
}
}
}
};
}
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.getacc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
結果如下:
參考文章:
http://developer.android.com/reference/android/accounts/package-summary.html