編輯:Android 開發實例
本文選自機械工業出版社《Android 應用開發揭秘》(作者:楊豐盛),聚焦 Android 2.0 新增的帳戶管理機制。
Android 2.0 加入了新套件 android.accounts,提供集中式的帳戶管理 API,用於安全地儲存與存取認證用的權杖與密碼。手機上可以存在多個帳戶,每個帳戶下有不同資訊,甚至每個帳戶都能與不同伺服器進行資料同步——例如手機帳戶中的聯絡人可對應某個 Gmail 帳戶的通訊錄,並連網同步更新。
以下結合一個範例程式學習 android.accounts 各功能的使用。範例實現帳戶添加,可集中管理多個帳戶。程式中建立了 4 個帳戶,因此退出後點擊新增聯絡人時,會提示使用者選擇在哪個帳戶中建立,讓各帳戶獨立隔開又統一管理(對應本書附錄碼:第9章\Examples_09_08)。
由於範例會操作帳戶資訊,必須先在 AndroidManifest.xml 宣告權限並確認 API 等級為 5:
<uses-sdk android:minSdkVersion="5"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<uses-permission android:name="android.permission.ACCOUNT_MANAGER"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
透過 AccountManager 的 get 方法取得物件:
AccountManager _am = AccountManager.get(this);
AccountManager 提供了眾多方法管理帳戶。例如列出指定類型的所有帳戶:
private void listAccounts()
{
Account[] accounts = _am.getAccountsByType(getString(R.string.ACCOUNT_TYPE));
_accountList.setText("帳戶列表:");
for (Account account : accounts)
{
_accountList.setText(_accountList.getText().toString() + '\n' + account.name + " - " + account.type);
}
}
重點在於如何把帳戶資訊加入管理器。首先實作一個 AccountAuthenticatorActivity 供使用者輸入帳戶資訊:
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 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();
}
});
}
}
先以帳戶名與類型建立 Account,再用 Bundle 加入伺服器資料,最後透過 addAccountExplicitly 把帳戶加入管理器。
接著建立 authenticator.xml:
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="https://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" />
並在 AndroidManifest.xml 啟動帳戶管理服務:
<service android:name="SleepyAccountsService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" ></action>
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator"></meta-data>
</service>
服務類別的實作很簡單,在 intent.getAction() 為 ACTION_AUTHENTICATOR_INTENT 時,透過 getIBinder() 回傳一個 IBinder:
public class SleepyAccountsService extends Service
{
private SleepyAccountAuthenticator _saa;
public IBinder onBind(Intent intent)
{
IBinder ret = null;
if (intent.getAction().equals(android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT))
ret = getSleepyAuthenticator().getIBinder();
return ret;
}
private SleepyAccountAuthenticator getSleepyAuthenticator()
{
if (_saa == null) _saa = new SleepyAccountAuthenticator(this);
return _saa;
}
}
最關鍵的是 AbstractAccountAuthenticator 的實作,因為新增與操作帳戶時會透過它進行非同步呼叫。addAccount 方法如下:
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException
{
Bundle ret = new Bundle();
Intent intent = new Intent(_context, SleepyAccountAuthenticatorActivity.class);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
ret.putParcelable(AccountManager.KEY_INTENT, intent);
return ret;
}
本章內容之間關聯不緊密,都是 Android 的特色功能,也正是這些功能吸引了眾多開發者與使用者,包含感測器、語音辨識、Google Map API、桌面組件開發與帳戶管理等。這些功能在日常生活中應用廣泛,例如 GPS 導航、路徑規劃等。帳戶管理較抽象,建議搭配範例程式與 Android 2.0 SDK 文件一起學習。
本文出自「yarin's blog」部落格。
Android 音樂播放器:MediaPlayer 播放與跳轉
若要在 Android App 中播放音樂,MediaPlayer 是最直接的選擇。它可以播放 raw 資源中的音訊檔,也能搭配 SeekBar 做進度控制。 載入音樂檔
文字繞圖排版實作:讓文字環繞圖片並自然接續
做內容型 App 時常會遇到這種版面:一張圖片靠左(或靠右),文字繞著它排;圖片下方的文字還要跟右側的文字連貫起來,讀起來像同一段話。這件事 TextView 本身做不到
Android 仿 QQ 聊天撒花特效實作,效果逼真
先看看效果圖吧! 要實現這樣的撒花特效,你需要了解貝塞爾曲線(Bézier Curve)——這正是整個動畫的核心。下面直接進入實作。 1. activity_main
Android ApiDemo 系列十:QuickContactsDemo 與 Content Provider
在 Android 的 ApiDemo 中,QuickContactsDemo 常用來示範如何透過 Content Provider 存取系統聯絡人資料。Content