一步一步教你在 Android 裏創建自己的賬號系統(一)

大家如果喜歡我的博客,請關注一下我的微博,請點擊這裏(http://weibo.com/kifile),謝謝

轉載請標明出處(http://blog.csdn.net/kifile),再次感謝


大家在平時使用 Android 手機的時候,都會發現有些應用(例如 qq,微信,淘寶)爲自己創建了賬號系統,並且能夠在設置頁面看到他,可是當自己希望爲自己的軟件寫一個賬號系統的時候總是不知從何入手,現在我們就從頭開始,一步一步打造屬於自己應用的賬號系統。


在進行設備賬戶管理的時候,我們會通過一個 AccountManager 類獲取系統的賬戶管理類,獲取的方法如下:

AccountManager mAccountManager = (AccountManager) getSystemService(ACCOUNT_SERVICE);

或者

AccountManager accountManager = AccountManager.get(context);

接下來我們需要通過 AccountManager 對象對賬號系統進行操作。

1.獲取賬戶信息

首先我們來查看一下如何獲取用戶已有的賬戶信息,如果你希望讀取系統當前的賬戶信息,那麼你首先需要在 manifest 文件中申明一個讀取賬戶的權限,如下:

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

(1)獲取所有賬戶信息

如果你希望獲取到當前設備所有的賬戶信息,你可以使用:

accountManager.getAccounts();

(2)獲取特定的賬戶信息

如果你只希望獲取自己或者特定的賬戶信息,你就應該使用:

accountManager.getAccountsByType("com.kifile");

後面的參數是你自己定義的賬戶類型,怎麼設置我會在接下來的文章中寫出來。

然後,我們就可以通過上面獲取到的工具類,讀取到手機上的賬戶信息了,這裏我寫了一個 ListView 的 Adapter 用於展示當前系統中的賬號信息:

public class AccountAdapter extends BaseAdapter {
        private Account[] mAccounts;

        public AccountAdapter(Account[] accounts) {
            this.mAccounts = accounts;
        }

        @Override
        public int getCount() {
            return mAccounts != null ? mAccounts.length : 0;
        }

        @Override
        public Object getItem(int position) {
            return mAccounts[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv = new TextView(getBaseContext());
            tv.setText(mAccounts[position].name + " " + mAccounts[position].type);
            return tv;
        }
    }

獲取當前所有系統賬戶信息以及設置 ListView 的 Adapter 的方法如下:

mListView.setAdapter(new AccountAdapter(mAccountManager.getAccounts()));

顯示效果如下:


同你當前設備進行對比,你就會發現我們已經將當前所有的賬戶信息顯示了出來,同樣地如果你只希望顯示部分的賬戶信息,你就可以通過 getAccountByType 獲取對應的賬戶列表。

2.建立自己的賬號服務

通過上面的部分,我們已經知道了如何獲取 Android 本身現有的賬號信息,現在我們就開始着手建立屬於自己的賬號系統吧。

你需要知道的是,如果你希望建立自己的賬號系統,那麼你得在 manifest 文件中聲明一個關於賬號的Service,如下

<service
                android:name="com.kifile.account.app.account.AccountService"
                android:enabled="true"
                android:exported="true">
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator"/>
            </intent-filter>
            <meta-data
                    android:name="android.accounts.AccountAuthenticator"
                    android:resource="@xml/authenticator"/>
        </service>

在上面的代碼中,我們通過設置 intent-filter 告知系統,我們當前應用中有一個賬號服務,至於具體的賬號信息則放在 meta-data 中的 android:resource 文件中提供, 該文件爲authenticator.xml,放置路徑爲 res/xml,內容如下:

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:accountType="com.kifile"
        android:icon="@drawable/ic_launcher"
        android:smallIcon="@drawable/ic_launcher"
        android:label="@string/app_name"/>

在這裏,我們就可以向系統提供相關的賬戶信息,用於在 Android Setting 目錄下顯示對應的賬號信息。例如這裏,我們就定義了當前的賬戶類型爲"com.kifile",在賬戶系統中顯示的標籤爲@string/app_name 對應的 String 對象,顯示的 icon 爲ic_launcher。

Ok,到了這裏,我們已經向系統聲明瞭一個賬戶相關的服務,現在讓我們來具體實現他。

Android 爲我們提供了一個叫做 AbstractAccountAuthenticator 的抽象類,也是通過它來實現:

public static class Authenticator extends AbstractAccountAuthenticator {


        public Authenticator(Context context) {
            super(context);
        }

        @Override
        public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
            return null;
        }

        @Override
        public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
            return null;
        }

        @Override
        public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
            return null;
        }

        @Override
        public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
            return null;
        }

        @Override
        public String getAuthTokenLabel(String authTokenType) {
            return null;
        }

        @Override
        public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
            return null;
        }

        @Override
        public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
            return null;
        }
    }
從上面的代碼中,我們可以看出,Android 需要從我們自定義的 Service 中獲取一個 AbstractAccountAuthenticator 對象,然後再調用對象中的方法來實現賬號系統的具體操作,每一個方法的意義,我們在本文中就不具體探討了,如果你有興趣,可以去看看 API 文檔。

當我們創建好了一個AbstractAccountAuthenticator 類後,我們需要從 Service 中取得這個類的對象,代碼如下:

private Authenticator authenticator;

    @Override
    public void onCreate() {
        super.onCreate();
        authenticator = new Authenticator(this);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return authenticator.getIBinder();
    }
AccountService 類在 onCreate 的時候創建一個 Authenticator 對象,然後再 bindService 的時候,將 Authenticator 的  IBinder 傳遞回去,以供調用。

當你完成上面的步驟之後,你就會發現,在你的設置頁面點擊添加賬戶時就會出現你自定義的賬戶了,如下:


3.添加賬戶

雖然通過上面的步驟,我們已經能夠在添加賬戶的界面看到屬於我們自己的賬戶類別了,但是你會發現當你點擊它們的時候,沒有任何作用,那麼我們應該怎麼在設備上完成添加賬戶的操作呢?

(1)加入添加賬戶的權限

添加賬戶也需要對應的權限,你應該在 manifest 文件中加入

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

(2)重寫 Authenticator 的 addAccount 方法

當用戶在添加賬戶頁面選擇賬戶進行添加或者調用accountManager.addAccount 的時候,系統會默認調用 AbstractAccountAuthenticator 中的 addAccount 方法,因此你需要重寫 addAccount 方法,直接添加默認賬戶,或者跳轉到某個頁面,讓用戶填寫用戶信息,然後添加賬戶。

(3)使用 addAccountExplicitly 直接添加賬戶

如果你希望直接添加賬戶信息,你可以使用以下方法:

Account account = new Account("Kifile,,,,12","com.kifile");
        accountManager.addAccountExplicitly(account,password,userdata);

使用之後,你就會發現在設置頁面出現你所建立的賬戶,點擊進去,會發現賬戶名什麼的也已經成功設置:



本文暫時就到這裏了,通過本文,你可以初步瞭解到 Android 的賬戶信息的建立流程,希望對大家有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章